gpt4 book ai didi

batch-file - 用于检查文件最后一行的 Windows 批处理脚本帮助

转载 作者:行者123 更新时间:2023-12-04 22:13:23 26 4
gpt4 key购买 nike

我有几百个文件,其中大部分在末尾(结束行)包含 '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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com