windows 批处理文件返回到文本文件的最后一行旁边
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4250401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
batch file to return next to last line of text file
提问by Nick Sinas
I have a file that contains the output of a file compare thats written to a text file:Comparing files C:\LOGS\old.txt and C:\LOGS\NEW.TXT
***** C:\LOGS\old.txt
***** C:\LOGS\NEW.TXT
folder_thats_different
*****
I need to pull out the next to last line "folder_thats_different" and put in a new string:folder contains a file that is different: folder_thats_different
我有一个文件,其中包含写入文本文件的文件比较的输出:
我需要拉出倒数第二行“folder_thats_different”并放入一个新字符串:Comparing files C:\LOGS\old.txt and C:\LOGS\NEW.TXT
***** C:\LOGS\old.txt
***** C:\LOGS\NEW.TXT
folder_thats_different
*****folder contains a file that is different: folder_thats_different
Yes, I know I can use another language, but I'm stuck with batch files for now.
是的,我知道我可以使用另一种语言,但我现在只能使用批处理文件。
采纳答案by jeb
You can try to read it with a for-loop and take the current line, and always save the previous line
您可以尝试使用for循环读取它并取当前行,并始终保存上一行
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%x in (myFile.txt) do (
set "previous=!last!"
set "last=%%x"
)
echo !previous!
回答by Patrick Gilliland
The first answer works for me. I also added 2 lines after the end to allow it to repeat so I could watch an active log file without having to close and reopen it. I do a lot of debugging for the mods that are used in the game Space Engineers. My version looks like this:
第一个答案对我有用。我还在末尾添加了 2 行以允许它重复,这样我就可以查看活动日志文件而无需关闭并重新打开它。我对 Space Engineers 游戏中使用的 mod 进行了大量调试。我的版本是这样的:
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%x in (SpaceEngineers.log) do (
set "previous=!last!"
set "last=%%x"
)
echo !previous!
timeout 15 /nobreak
se_log
The line below stops the batch file from looping too fast and stop the key bypass. To change the amount of time in seconds just change the number "15" to anything you want. To stop the batch file just press ctrl+c.
下面的行阻止批处理文件循环太快并停止密钥绕过。要以秒为单位更改时间量,只需将数字“15”更改为您想要的任何值。要停止批处理文件,只需按ctrl+ c。
timeout 15 /nobreak
The line below is the name of the batch file I made so it will tell CMD to run this again.
下面的行是我制作的批处理文件的名称,因此它会告诉 CMD 再次运行它。
se_log
回答by Michael Burr
Here's an example you can use as a starting point. Just change the filename in the set command=
line to the appropriate name (or replace the command with whatever will gerneate the log listing).
这是您可以用作起点的示例。只需将行中的文件名更改为set command=
适当的名称(或将命令替换为将生成日志列表的任何内容)。
@echo off
@setlocal
(set command=type test.txt)
for /f "usebackq tokens=*" %%i in (`%command%`) do call :process_line %%i
echo next to last line: %old_line%
goto :eof
:process_line
(set old_line=%new_line%)
(set new_line=%*)
goto :eof
Of course, you'll probably want to do something other than simply echoing the found line.
当然,除了简单地回显找到的行之外,您可能还想做一些其他的事情。