批处理文件返回文本文件最后一行的旁边

发布于 2024-10-04 00:55:33 字数 312 浏览 2 评论 0原文

我有一个文件,其中包含写入文本文件的文件比较的输出:
比较文件 C:\LOGS\old.txt 和 C:\LOGS\NEW.TXT
***** C:\LOGS\old.txt
***** C:\LOGS\NEW.TXT
文件夹_thats_不同
*****

我需要拉出倒数第二行“folder_thats_ Different”并放入一个新字符串:
文件夹包含一个不同的文件:folder_thats_ different

是的,我知道我可以使用另一种语言,但我现在只能使用批处理文件。

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

Yes, I know I can use another language, but I'm stuck with batch files for now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坏尐絯℡ 2024-10-11 00:55:33

您可以尝试使用 for 循环读取它并获取当前行,并始终保存上一行

@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%x in (myFile.txt) do (
    set "previous=!last!"
    set "last=%%x"
)
echo !previous!

You can try to read it with a for-loop and take the current line, and always save the previous line

@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%x in (myFile.txt) do (
    set "previous=!last!"
    set "last=%%x"
)
echo !previous!
來不及說愛妳 2024-10-11 00:55:33

这是一个可以作为起点的示例。只需将 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

当然,您可能想做的不仅仅是简单地回显找到的行。

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).

@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.

月下凄凉 2024-10-11 00:55:33

第一个答案对我有用。我还在末尾添加了 2 行以允许其重复,这样我就可以观看活动日志文件,而无需关闭并重新打开它。我对《太空工程师》游戏中使用的模组进行了大量调试。
我的版本如下所示:

@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

下面的行阻止批处理文件循环太快并停止密钥绕过。要更改以秒为单位的时间量,只需将数字“15”更改为您想要的任何值。要停止批处理文件,只需按 ctrl+c

timeout 15 /nobreak

下面的行是我创建的批处理文件的名称,因此它会告诉 CMD 再次运行它。

se_log

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:

@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.

timeout 15 /nobreak

The line below is the name of the batch file I made so it will tell CMD to run this again.

se_log
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文