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 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的出现,但我想对文本文件的每一行执行相同的操作。

怎么做?

4

1 回答 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
于 2013-11-04T07:27:35.840 回答