批量解析字符串输出

时间:2014-02-26 19:20:26

标签: batch-file command-line cmd

我通过在批处理程序中调用avp.com来扫描卡巴斯基输出。我在名为RES的变量中输出此输出,该变量的内容为:

enter image description here

我需要解析此字符串以获取检测到的总数值,在上面的示例中为 0 。我已批量尝试使用 for 语句,但没有按预期获得结果。

注意:使用@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:          
;  ------------------

任何帮助都会有所帮助。感谢

2 个答案:

答案 0 :(得分:3)

@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(也可以使用{{1} }

答案 1 :(得分:2)

您可以通过管道FINDSTR获取所需的行:

set KAVDir="C:\Program Files (x86)\Kaspersky Lab\Kaspersky Endpoint Security 10 for Windows\avp.com"
for /f "eol=# tokens=4 delims= " %%i in (
    '%KAVDir% /i0 %1 ^| FINDSTR /C:"Total detected"'
) do (
    set RES=%%i
)

修改:正如Magoo指出的那样,我需要添加/C来搜索确切的字符串“检测到的总数”,并在eol中添加for {1}}循环,默认情况下会跳过以;开头的行。