从批处理文件中删除文本行

时间:2013-10-11 02:28:25

标签: batch-file

我已经在网上搜索了好几天,却找不到我要找的东西。

我正在逐行读取文件,抓取第一个文件名,然后运行该文件。运行文件后,我会将其移动到存档文件夹,并在读取下一行之前从文件中删除文本行。

我需要在文件运行后删除该行文本,但无法弄清楚如何执行该操作

@ECHO OFF
REM Get time date in special format
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c%%a%%b)
For /f "tokens=1-2 delims=/: " %%a in ("%TIME%") do (set mytime=%%a%%b)

REM Loop through each line
FOR /F "tokens=1-3 delims=, " %%i IN (./ready/input.txt) DO (

    REM>output.txt

    REM Run the file
        call .\ready\%%i || goto :error

    REM Move the file to archive folder
    move .\ready\%%i .\archive\%mydate%_%mytime%_%%j_%%k_%%i

    REM ***Remove the current line of text from input.txt ****

)
goto :EOF

:error
echo Failed with error #%errorlevel%.
exit /b %errorlevel%

编辑:更多信息。我正在尝试做的是跟踪未运行的文件...所以可能有6行文件,它可能读取第1行运行文件,读取第2行运行文件,然后找不到文件3所以它退出...我需要能够输出最后4行

1 个答案:

答案 0 :(得分:1)

@ECHO OFF
REM Get time date in special format
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c%%a%%b)
For /f "tokens=1-2 delims=/: " %%a in ("%TIME%") do (set mytime=%%a%%b)

REM Loop through each line
set "errorfound="
del newfile.txt >nul 2>nul
FOR /F "tokens=1-3 delims=, " %%i IN (./ready/input.txt) DO (

 REM YOU DO REALISE THIS NEXT LINE WILL RE-CREATE THE FILE EACH ITERATION DON'T YOU?
 REM>output.txt

 REM Run the file
 if not defined errorfound (
  call .\ready\%%i
  if errorlevel 1 (
   REM PRESUME ERRORLEVEL1+=fail
   set errorfound=Y
   ) else (
    REM Move the file to archive folder
    move .\ready\%%i .\archive\%mydate%_%mytime%_%%j_%%k_%%i
  )
 )
 if defined errorfound >>newfile.txt echo %%i %%j %%k
)

if defined errorfound echo error found-unprocessed lines in newfile.txt
goto :EOF

假设您想要停止第一个错误,上述情况应该有效。

errorfound一旦设置,就会发现错误。 if defined适用于变量的当前值,因此在找到第一个错误后,它会将输入数据行的重要部分再现为新文件。