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

标签 batch-file window

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

相关文章:

javascript - 在窗口调整大小时重新加载 jQuery Carousel 以将方向从垂直更改为水平

javascript - $(window).height() 值在 Firefox 22.0 中是常量

visual-studio-2010 - 如何从批处理文件执行 'msbuild'命令

windows - 为什么 exit/b 从 Jenkins(但其他任何地方)调用时不能正常工作?

PowerShell错误: Cannot move item because the item at xxx is in use

Javascript:查找使用相同域打开的浏览器窗口

javascript - 在 Web 应用程序中为分离的面板使用实际的浏览器窗口是否可行?

windows - 如何使用批处理文件从 COM 端口读取数据?

powershell - 将参数从 bat 文件传递​​给 PowerShell

php - 如何从 PHP 运行 .bat 文件?