Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
I am trying to create a batch file to parse a log file, find some timestamps in a specific format, change the format of the timestamp, then display the timestamp in the command window. My current code is working exactly as I'd like it to, but I am running into a problem with a specific line in my log file. It seems my code doesn't like the line with the parentheses. How can I adjust my code to make it read past this line?
----Start batch file code-------
@echo off
:parse_log
setLocal EnableDelayedExpansionfor /f "eol=> tokens=* delims= " %%a in (%1) do (
set for_line=%%a
call :parse
):parse
echo %for_line% | find "TIMESTAMP" >NUL
if not errorlevel 1 (
set str=%for_line%
call :split_string
)
goto :EOF:split_string
:: Find the time part of the timestamp
for /f "tokens=2,3 delims=^-" %%a in ('echo !str!') do (
set timestamp=%%a
set timestamp_comment=%%b
set finaltimestamp=!timestamp:~1,11!
set finaltimestampcomment=!timestamp_comment:~2,-2!
)echo !finaltimestamp! - !finaltimestampcomment!
goto :EOF
----End batch file code---------Start example log file text-----
lots of text I don't care about...
lots of text I don't care about...
lots of text I don't care about...> !batch_currenttime.bat "Domain reordered"
TIMESTAMP - 14:10:04.02 - "Domain reordered"> /define/models/radiation/s2s-parameters/read-vf-file example.cas.gz.s2s.gz
Reading "| gunzip -c "D:/Projects/2008/Fluent_Radiaton/Raytracing2/testing/smalltest/example.cas.gz.s2s.gz""...
cells........
Reading radiating clusters (7463) and view factor values from D:/Projects/2008/Fluent_Radiaton/Raytracing2/testing/smalltest/example.cas.gz.s2s.gz...
,
Initialized VF Storage
Done.> !batch_currenttime.bat "Starting 1st order iterations"
TIMESTAMP - 14:10:39.56 - "Starting 1st order iterations"
----End example log file text-----

This doesn't so much fix the problem as avoid it. Maybe IVO can show an elegant solution.
Note that you omitted a goto :eof after your first FOR loop, causing it to stutter.
::==
@echo off > clean
:parse_log
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in (%1) do (
::echo %%a
set str=%%a
set str=!str:^(=[!
set str=!str:^)=]!
echo !str!>> clean
)
for /f "eol=> tokens=* delims= " %%a in (clean) do (
set for_line=%%a
call :parse
)
goto :eof
:parse
echo %for_line% | find "TIMESTAMP" >NUL
if not errorlevel 1 (
set str=%for_line%
call :split_string
)
goto :EOF:split_string
:: Find the time part of the timestamp
for /f "tokens=2,3 delims=^-" %%a in ('echo !str!') do (
set timestamp=%%a
set timestamp_comment=%%b
set finaltimestamp=!timestamp:~1,11!
set finaltimestampcomment=!timestamp_comment:~2,-2!
)echo !finaltimestamp! - !finaltimestampcomment!
goto :EOF
=====================================
If at first you don't succeed, you're about average.M2

One nit-picky note: you trimmed the end quote AND the last ;etter of the string, yielding:
14:10:04.02 - Domain reordere
14:10:39.56 - Starting 1st order iteration
=====================================
If at first you don't succeed, you're about average.M2

More imply:
@echo off > newfile
setLocal EnableDelayedExpansionfind "TIMESTAMP" < %1 > %temp%\##
for /f "tokens=2* delims=^- " %%a in (%temp%\##) do (
set str=%%a - %%b
set str=!str:"=!
echo !str! >> newfile
)
=====================================
If at first you don't succeed, you're about average.M2

version 3:
@echo off > newfile
setLocal EnableDelayedExpansionfor /f "tokens=2* delims=^- " %%a in ('find "TIMESTAMP" ^< %1') do (
set str=%%a - %%b
set str=!str:"=!
echo !str! >> newfile
)
=====================================
If at first you don't succeed, you're about average.M2

Thanks M2 for your help! The code works great. I'm still trying to figure out exactly what you did, but thank you! Also, the trimming of the last quote works fine on my system (with the last letter intact)... maybe it was due to my cut/paste of the log file into the first post.
Thanks again for your help!

M2's got the best solution, but just for the record, the reason your original script was broken was this line inside a for loop:
set str=%for_line%
When for_line contained the line with the brackets, the command interpreter took the closing bracket to mean the end of the for loop. Remember, "%" expansion is done before the line is parsed. You should use delayed expansion inside for loops (i.e. use "!" instead of "%".)

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |