0

我在网上搜索了几天,但没有找到我要找的东西。

我正在逐行读取文件,获取作为文件名的第一个标记,然后运行该文件。运行文件后,我会将其移动到存档文件夹并从文件中删除该行文本,然后再读取下一行。

我需要在文件运行后删除那行文本,但不知道该怎么做

@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 行

4

1 回答 1

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处理变量的当前值,因此在发现第一个错误后,它将输入数据行的重要部分复制到新文件中。

于 2013-10-11T03:53:29.627 回答