我有几百个文件,其中大多数exit
在末尾(结束行)包含“ ”。并非所有文件exit
末尾都有这个“ ”。如何检查文件最后一行的内容然后删除如果是' exit
'呢?
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)
编辑:工作并有一点改进。仍然无法处理空行,但现在好多了。
归档时间: |
|
查看次数: |
2440 次 |
最近记录: |