2

I am trying to replicate a shell script within windows bacth. I am getting close but am currently stuck on trying to substring a datetime token to output just the time.

    @echo off
echo."Location","Date Time","Result" > output2.csv
( for %%a in (z:\logdir\*.log) do (
for /f "tokens=1,2,3,4,5,6,7,8,9,10 delims=[+]" %%B in ('findstr /g:searchstrings.txt  ^< %%a') do (
set time=%%B
set time1=%time:~-6%
echo."%%a","%_time1%","%%B","%%F") 
)
) >> output2.csv

the log file contains many entries but this script should and does pull out just teh following lines

[20110314T103852][EMVLib][5056][I000000]:  Verification: SUCCESS

[20110314T103902][CSV][3232][D000000]: SendResponse: Response message

These search strings are defined by file searchstrings.txt

so in short my script currently outputs a csv with the log name, date/time stamp [20110314T103852] and the message (Verification: SUCCESS and SendResponse: Response message.

but what I want is for it to output just the time element to the second column.

Ideally this script then needs to work out the time difference for each element but god only know how (yes thats a question)

Thanks for any help on this!

user639410
  • 387
  • 1
  • 3
  • 9
  • Btw. if you don't need the other tokens, you can omit them `for /f "tokens=1,5" %%B` then the map is: token1 in %%B, token5 in %%C – jeb Apr 01 '11 at 11:15
  • There is a typo in `echo %_time1%`, the var name is `time1`, perhaps it's the cause you always get `03:42.5` – jeb Apr 01 '11 at 15:04

1 Answers1

3

The key is here the delayed variable expansion.
!var! instead of %var%, as the delayed expansion expands just if the line is executed, the percent expansion expands in the moment of parsing, in your case the complete for-block is parsed first before executed.
So the line set time1=%time:~-6% can't work, because it expands before the time-var is set.

@echo off
setlocal EnableDelayedExpansion
echo."Location","Date Time","Result" > output2.csv
( 
  for %%a in (z:\logdir\*.log) do (
    for /f "tokens=1,2,3,4,5,6,7,8,9,10 delims=[+]" %%B in ('findstr /g:searchstrings.txt  ^< %%a') do (
      set time=%%B
      set time1=!time:~-6!
      echo."%%a","!time1!","%%B","%%F"
    ) 
  )
) >> output2.csv
jeb
  • 78,592
  • 17
  • 171
  • 225
  • Thanks for the very quick response. I had just started reading about delayed expansion but it seems to be missing the mark slightly as when I run the script now the time is outputted as 03:42.5 for all lines – user639410 Apr 01 '11 at 11:18
  • You should change the `echo off` to `echo on` and remove the redirection to output2.csv, so you can see what happens. However `!time:~-6!` takes the last 6 characters, on my system I get `:49.12` for 49 seconds and 120 milli seconds – jeb Apr 01 '11 at 11:29
  • aha, OK so I assume thats because time is reserved, so I jsut need to use another variable name to return the actual text. I think thats the answer. Thanks soo much Jeb! – user639410 Apr 01 '11 at 11:52
  • Do you have any idea how I might then calculate the time difference between the start and end times of each pair? – user639410 Apr 01 '11 at 11:53
  • This seems to be a new question, but anyway you should calculate the total seconds for each timestamp and subtract them. – jeb Apr 01 '11 at 12:03
  • Sorry it was a new question. I'll have to repost that specific question as I have no idea – user639410 Apr 01 '11 at 12:04
  • Actually its not a new question, How can I get the timestamp eg:49.12 as well as the literal string using the delayed expansion method? If I try now to output the timestamp using the above method it agian writes out the same timestamp for all rows – user639410 Apr 01 '11 at 13:59