batch-file - 批量解析字符串输出

标签 batch-file command-line cmd

我通过在批处理程序中调用 avp.com 获得了来自扫描的卡巴斯基输出。我在一个名为 RES 的变量中有这个输出,这个变量的内容是:
enter image description here
我需要解析这个字符串才能得到 检测到的总数 值,在上面的例子中是 0 .我试过 批处理语句,但没有得到预期的结果。
注意:用@mbroshi的anwser编辑

set KAVDir="C:\Program Files (x86)\Kaspersky Lab\Kaspersky Endpoint Security 10 for Windows\avp.com"
for /f "tokens=3 delims= " %%i in ('%KAVDir% /i0 %1 ^| FINDSTR "Total detected"') 
do 
( 
SET RES=%%i 
ECHO %RES% 
)
avp.com 程序执行的输出文本是:
2014-02-26 15:01:58 Scan_Objects$0697         starting   1%         
; --- Settings ---
; Action on detect:     Ask user
; Scan objects:         All objects
; Use iChecker:         Yes
; Use iSwift:           Yes
; Try disinfect:        Yes
; Try delete:           Yes
; Try delete container: No
; Exclude by mask:      No
; Include by mask:      No
; Objects to scan:      
;   "D:\Codigo\Git_Segmail\mail_filters\segmail20\server\service_node_helpers\antivirusscripts\eicar.com.txt"   Enable=Yes  Recursive=No
; ------------------
2014-02-26 15:01:58 D:\Codigo\Git_Segmail\mail_filters\segmail20\server\service_node_helpers\antivirusscripts\eicar.com.txt:Zone.Identifier ok
Progress 50%...
2014-02-26 15:01:58 D:\Codigo\Git_Segmail\mail_filters\segmail20\server\service_node_helpers\antivirusscripts\eicar.com.txt detected    EICAR-Test-File
Progress 50%...
2014-02-26 15:01:58 Scan_Objects$0697         running    50%        
2014-02-26 15:02:00 D:\Codigo\Git_Segmail\mail_filters\segmail20\server\service_node_helpers\antivirusscripts\eicar.com.txt was deleted
2014-02-26 15:02:00 Scan_Objects$0697         completed             
;  --- Statistics ---
;  Current time:    2014-02-26 15:02:00
; Time Start:           2014-02-26 15:01:58
; Time Finish:          2014-02-26 15:02:00
; Completion:           100%
; Processed objects:    2
; Total detected:       1
; Detected exact:       1
; Suspicions:           0
; Treats detected:      1
; Untreated:            0
; Disinfected:          0
; Quarantined:          0
; Deleted:              1
; Skipped:              0
; Archived:             0
; Packed:               0
; Password protected:   0
; Corrupted:            0
; Errors:               0
; Last object:          
;  ------------------

最佳答案

@ECHO OFF
SETLOCAL
set KAVDir="C:\Program Files (x86)\Kaspersky Lab\Kaspersky Endpoint Security 10 for Windows\avp.com"
FOR /f "eol=#tokens=2delims=:" %%a IN ('%KAVDir% /i0 %1 ^|find "Total detected"') DO SET /a totdet=%%a
ECHO Total detected is %totdet%
GOTO :EOF

请注意 findstr "Total detected"将找到任何包含“总计”的行 “检测到”,因此使用 FIND (也可以用 findstr /c:"Total detected" 完成)

关于batch-file - 批量解析字符串输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22051119/

相关文章:

windows - 如何使用 ExecDos 命令隐藏命令提示符窗口

windows - 查找在日期之间创建/访问/修改的文件,批处理脚本

date - 将修改后的文件日期与批处理文件中的当前日期进行比较

macos - Android studio 从 mac 终端运行 lint

c# - 如何通过命令行在远程机器上触发批处理文件

windows - 仅在出错时解析命令的输出,并且仅使用 native 批处理命令

email - 支持 GMail TLS/STARTTLS 与 SSL 的 Curl SMTP 命令行参数是什么

linux - 从属模式下类似于 mplayer 的命令行音频播放器

c++ - 使用 QProcessEnvironment 更改 cmd.exe 的 PATH 环境变量

android - 用于分割二进制文件的 Windows 命令