Windows 批处理脚本帮助检查文件的最后一行

Raj*_*ena 5 window batch-file

我有几百个文件,其中大多数exit在末尾(结束行)包含“ ”。并非所有文件exit末尾都有这个“ ”。如何检查文件最后一行的内容然后删除如果是' exit'呢?

npo*_*aka 2

   setlocal enabledelayedexpansion 
   for /f "delims=" %%Z in ('dir /s /b /a-d .') do (
       for /f "delims=" %%a in (%%Z) do  set last_line=%%a
       echo !last_line! | find "exit" && del /f /q %%Z
    )
    endlocal
Run Code Online (Sandbox Code Playgroud)

编辑(foxidrive读得比我仔细):

  @echo off

  setlocal enabledelayedexpansion 
   for /f "delims=" %%Z in ('dir /s /b /a-d') do (

       set /a counter=1
       set "yes=no"
       for /f "usebackq delims=" %%a in ("%%Z") do  set last_line=%%a&& set /a counter=counter+1
       echo !last_line! | find /I "exit" >nul&& set "yes=yes"
       if "!yes!" equ "yes" (
          break >"%%Z.tmp"
          for /f "usebackq delims=" %%a in ("%%Z") do (
             set /a counter=counter-1
             if "!counter!" neq "1" (
                endlocal
                echo %%a | find "ECHO is off.">nul&&echo\>>"%%Z.tmp"   
                echo %%a | find "ECHO is off.">nul||echo %%a>>"%%Z.tmp"
            )
          )
          echo "Exit" found in  at the end of --%%Z--
          copy /y "%%Z.tmp" "%%Z" >nul 
       )
    )
    endlocal
Run Code Online (Sandbox Code Playgroud)

编辑:工作并有一点改进。仍然无法处理空行,但现在好多了。

  • 我认为目标是删除“exit”而不是删除文件。 (2认同)