我有几百个文件,其中大部分在末尾(结束行)包含 'exit
'。并非所有文件的末尾都有这个 'exit
'。如何检查文件最后一行的内容以及如果它是“exit
”,则将其删除?
最佳答案
@echo off
setlocal EnableDelayedExpansion
rem "I have a few hundred files"
for %%a in (*.*) do (
rem "most of which contains 'exit' at the end line."
set "lastLine="
(for /F "usebackq delims=" %%b in ("%%a") do (
if not defined lastLine (
set "lastLine=%%b"
) else (
echo(!lastLine!
set "lastLine=%%b"
)
)) > "%%~Na.tmp"
rem "Not all of the files have this 'exit' at the end."
rem "How can I check the content of last line of the files"
if "!lastLine!" equ "exit" (
rem "and then remove it if it's 'exit'?"
move /Y "%%~Na.tmp" "%%a"
) else (
del "%%~Na.tmp"
)
)
关于batch-file - 用于检查文件最后一行的 Windows 批处理脚本帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16672477/