开发者

set variable from token in a for loop

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 (Verif开发者_如何学Pythonication: 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!


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜