Reputation: 33
@echo off
rem Let findstr to find the LINE you want (only once):
for /f "delims=" %%a in ('findstr "to_timestamp" t1.txt') do set "_line=%%a"
SET _line2=%_line:*to_timestamp=%
SET _line3=%_line2:*)=%
set _rep_str=('07-10-13 07:07:29','%%d-%%m-%%y %%h:%%i:%%s')
CALL SET _result=%%_line:%_line2%=%%
CALL SET _result2=%%_line2:%_line3%=%%
CALL SET _result=%%_line:%_line2%=%%
CALL SET _result=%_result:to_timestamp=STR_TO_DATE%
echo %_result%%_rep_str%%_line3% >> Output.txt
::echo %_rep_str% >> Output.txt
::echo %_line3% >> Output.txt
PAUSE
Above code is working for the first line // occurrence of findstr but I want to perform the same operation on each line of the text file.
How to do that?
Upvotes: 1
Views: 4014
Reputation: 70923
If there is more than one line that matchs, the way you have coded the file, only the last occurrence will be processed (previous values are overwritten during loop)
Separate you code, create a subroutine and call this subroutine for each occurrence
@echo off
rem Let findstr to find the LINE you want (only once):
for /f "delims=" %%a in ('findstr "to_timestamp" t1.txt') do call :doWork "%%a"
....
....
PAUSE
exit /B
:doWork
SET "_line=%~1"
SET _line2=%_line:*to_timestamp=%
SET _line3=%_line2:*)=%
set _rep_str=('07-10-13 07:07:29','%%d-%%m-%%y %%h:%%i:%%s')
CALL SET _result=%%_line:%_line2%=%%
CALL SET _result2=%%_line2:%_line3%=%%
CALL SET _result=%%_line:%_line2%=%%
CALL SET _result=%_result:to_timestamp=STR_TO_DATE%
echo %_result%%_rep_str%%_line3% >> Output.txt
::echo %_rep_str% >> Output.txt
::echo %_line3% >> Output.txt
goto :EOF
Upvotes: 1