2014-02-26 24 views
4

我通過在批處理程序中調用avp.com從掃描中獲得卡巴斯基輸出。我有一個變量名爲RES這個輸出,這個變量的內容是:解析批處理中的字符串輸出

enter image description here

我需要解析此字符串得到共檢測值,在上面的例子是。我已經批量嘗試了聲明,但沒有按預期得到結果。

注:與@mbroshi

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程序的執行輸出的文本編輯anwser是:

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

+0

是的 - 忘了那個'/ c'。更重要的是,我忘了默認的eol是';'! – mbroshi

+0

謝謝你的回覆,特別是這個幫我解決了問題,然後我知道另一個問題是什麼。感謝@mbroshi和@Magoo! – yosbel

+0

請注意,對於這個示例,工作正常,需要在%KAvdir%之前添加**調用**語句。 – yosbel

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 
) 

編輯:由於脫線所指出的,我需要添加一個/C搜索的確切字符串「總檢出」並在for循環中添加了一個不同的eol,因爲默認情況下它將跳過以;開頭的行。

+0

感謝POR的答覆@mbroshi,我不要做任何線不知道爲什麼,但是設置的RES值是一個空字符串,我應該嘗試別的方法嗎?謝謝 – yosbel

+0

@yosoy您可以發佈該行的實際文本到您的問題嗎?我將分隔符設置爲空格並抓取第三個標記,例如,'%KAVdir%'變量中可能有標籤 – mbroshi

+0

我編輯了putti ng .bat文件中的確切文本。我添加了通話聲明,但仍然沒有很好的結果。我也通過調用這個調用語句的輸出並將輸出保存到一個文件中。 – yosbel