2017-06-17 187 views
1

我使用下面的命令提取的文本文件的最后一行:批处理文件变量

for /f "tokens=*" %%m in (message_log.txt) do (
    Set lastline=%%m 
) 

我的目标是,如果变量%lastline%=="☺§☻PDF文件已被中止。 然后显示一个输出,如果没有退出。但我认为前三个角色已经搞乱了。我想这一点:

for /F "tokens=1-5 delims= " %%a in (%lastline%) do (
    if %%e==aborted. (
     echo pdf not filed 
    ) 
Pause 

但该文件只是退出,没有停顿,没有输出。

我可以得到这个工作,而不是使用%lastline%我指的是一个文件,就像我在第一个循环中所做的那样,但是我无法让它与变量一起工作。

使用FOR循环在预定义变量内进行搜索的正确语法是什么?

如果是简单我的最终目标是如果我的文本文件的最后一行包含字符串“中止”,以呼应的错误消息。有没有更好的方法来做到这一点?

+5

你缺少一个')'。打开命令提示符并从那里运行脚本,而不是双击它以查看是否有任何错误。 – SomethingDark

回答

0

你的第一种方法是好的,只是缺少检查。

for /f "delims=" %%m in (message_log.txt) do Set lastline=%%m 

If "%lastline%" neq "%lastline:abort=%"^
    Echo error message the last line in message_log.txt contains the string "abort" 

随着FINDSTR

for /f "delims=" %%m in (message_log.txt) do Set lastline=%%m 

Echo %lastline%|Findstr /i "abort" 2>&1 >Nul &&^
    Echo error message the last line in message_log.txt contains the string "abort" 

用的GnuWin32工具安装

tail -n 1 message_log.txt|grep "abort" >NUL &&^
    Echo error message the last line in message_log.txt contains the string "abort" 
+0

非常感谢您的解决方案!我用第一个,它工作太棒了!但我确实必须摆脱帽子。我不确定它为什么在那里,但它不会识别下一行代码是否存在。 – Lawrence

+0

作为最后一个字符的插入符号'^'是[line continuation char to split long lines](https://stackoverflow.com/questions/69068/long-commands-split-over-multiple-lines-in-windows- Vista的批处理BAT文件) – LotPings