批处理文件-在遍历多个文本文件时,一旦检测到关键字,如何获取关键字并执行操作(IF)? [英] Batch File - How to get a Keyword and perform action (IF) once the keyword detected while looping through multiple text files?

查看:46
本文介绍了批处理文件-在遍历多个文本文件时,一旦检测到关键字,如何获取关键字并执行操作(IF)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过使用文本文件内的关键字在一个文件夹内遍历多个文本文件(逐行读取文本文件)时执行操作(IF语句).我能够遍历多个文本文件.但是,我只得到最后一个文本文件关键字".我不知道为什么会这样.我的预期结果是,我能够获取文本文件中的每个关键字,而在检测到关键字时,它将执行IF语句,在IF语句之后,它将继续遍历其他文本文件并寻找另一个关键字.

I trying to perform action (IF statement) when looping through multiple text files( read line by line of text file) inside a folder by using the Keyword inside the text file. I able to loop through multiple text files. However I only get the last text file Keyword. I do not know why is this happening. My expected result is that I able to get every keyword inside the text file, while the Keyword is detected it will perform the IF statement, after IF statement then it will continue to loop through other text file and looking for another keyword.

例如.Log_DP_123.txt内容.

Eg. Log_DP_123.txt content.

ERROR-D12345678

例如.Log_DP_124.txt内容.

Eg. Log_DP_124.txt content.

DisConnected

我的代码:

@echo on
SETLOCAL EnableDelayedExpansion
for %%x in (C:\test\Scriptlogs\COB\log_DP_*.txt
) do for /f "usebackq delims=" %%a in ("%%~fx") do SET e=%%a
echo %e%
SET x=%e%
SET s=!x:~0,5!
SET date=!x:~6,14!
SET !d!
SET !s!
pause
IF "%s%" == "DisCo" (
(
echo disconnected
)
IF "%s%" == "ERROR" (
echo Sucess
echo %s%
echo %date%
)
)>C:\Users\310152922\Desktop\Mohit_Task\AutomatedScript\COB\sucess.txt

pause

我需要进行任何更改才能达到预期的结果吗?

Any changes that I need to make to meet my expected result?

预期结果-在sucess.txt中,它将列出所有回显.例如.sucess.txt中的内容.

Expected result - In sucess.txt it will list all the echo's. Eg. Contents inside sucess.txt.

Sucess
Error
12345678
disconnected

感谢您查看,评论和回答.

Thanks for viewing, comments and answers.

尽管我接受的答案与Post不同.但是我发现Aacini提供的方法简单,易懂,并且对我非常有用.所以我接受了这个答案.P.s.如果您要寻找此职位的答案,那应该是Dbenham的答案!

Although the answer that I accepted is different from Post. But I found out the method that Aacini provide is simple, easy understanding and very useful to me. So I accepted that answer. P.s. If you looking for this post answer, it should be Dbenham answer!

推荐答案

恐怕我不明白您的代码试图做什么.当您寻求帮助时,您总是应该用文字解释代码应该执行的操作;否则,我们如何为您提供帮助?例如,根据您的代码,您似乎并不在乎在哪个文件中显示关键字(您永远不会显示 %% x 的值).

I am afraid I don't understand what your code tries to do. When you ask for help, you always should explain in words what the code is supposed to do; otherwise, how we could help you? For example, based on your code it seems that you don't care in which file the keywords appear (you never show the value of %%x).

无论如何,如果要搜索多个文件以查找以"DisCo"或"ERROR"字符串开头的行,则可以使用以下命令:

Anyway, if you want to search several files looking for lines that start in "DisCo" or "ERROR" strings, then the following command do that:

findstr /B "DisCo ERROR" C:\test\Scriptlogs\COB\log_DP_*.txt

此命令显示具有某些附加信息(例如文件名)的匹配行,但是您可以使用 for 命令(以冒号作为定界符)来处理这些行.

This command show the matching lines with some additional information (like the name of the file), but you may process these lines with a for command taking the colon as delimiter.

我希望它能对您有所帮助...

I hope it helps...

编辑

我接受了先前的 findstr 命令并将其插入您的代码中,但已对其进行了相应的修改:

I taken previous findstr command and inserted it in your code, but modified it accordingly:

@echo off
setlocal EnableDelayedExpansion

for /F "tokens=2 delims=:" %%e in ('findstr /B "DisCo ERROR" log_DP_*.txt') do (
   REM echo %%e
   SET x=%%e
   SET s=!x:~0,5!
   SET ddate=!x:~7,14!
   IF "!s!" == "DisCo" (
      echo disconnected
   ) else (
      echo Sucess
      echo !s!
      echo !ddate!
   )
)

例如.Log_DP_123.txt内容:

Eg. Log_DP_123.txt content:

Any other line
ERROR-D12345678
Any other line

例如.Log_DP_124.txt内容:

Eg. Log_DP_124.txt content:

Any other line
Any other line
DisConnected
Any other line

输出:

Sucess
ERROR
12345678
disconnected

这篇关于批处理文件-在遍历多个文本文件时,一旦检测到关键字,如何获取关键字并执行操作(IF)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆