2013-11-04 266 views
0
@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 

上面的代碼是工作的FINDSTR第一行//發生,但我想在文本文件中的每一行執行相同的操作。批處理文件循環使用FINDSTR

如何做到這一點?

+1

你能給我們一些樣本輸入和期望的輸出嗎?可以更容易地理解你的問題。 –

回答

1

如果有一個以上的行配襯,已經編寫了文件的方式,只有最後出現的將被處理(前值循環期間被覆蓋)

分開你的代碼,創建一個子程序調用這個子程序每次出現

@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 
+0

謝謝...問題用你的方法解決。 –