Parse an string output in batch
Solution 1
@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
Note that findstr "Total detected"
will find any lines containing "Total" or "detected", hence the use of FIND
(could also have been done with findstr /c:"Total detected"
Solution 2
You can pipe to FINDSTR
to get the line you need:
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
)
Edit: As pointed out by Magoo, I needed to add a /C
to search for the exact string "Total detected" and added a different eol
in the for
loop, as by default it will skip lines beginning with a ;
.
yosbel
I'm a Computer's Science graduated. Work in a company of Computer Security.
Updated on August 21, 2022Comments
-
yosbel over 1 year
I have a Kaspersky output from an scan by calling avp.com in a batch program. I have this output in a variable named RES, the content of this variable is:
I need to parse this string to get the Total detected value, in the above example is 0. I have tried with the for statement in batch, but not getting the result as expected.
Note:Edited with the anwser of @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% )
The text of the output of an execution of the avp.com program is:
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: ; ------------------
-
yosbel about 10 yearsThanks por the reply @mbroshi, I don't know why but the RES value that is setted is an empty string. Should I try something else? Thanks
-
mbroshi about 10 years@yosoy Can you post the actual text of the line to your question? I'm setting the delimiter to space and grabbing the third token in the above script. E.g., perhaps there are tabs in
%KAVdir%
variable. -
yosbel about 10 yearsI have edited the question putting the exact text I have in the .bat file. I added the call statement, but still with no good results. I also put the output of the call statement by calling this one and saving the output to a file.
-
mbroshi about 10 yearsYep--forgot about that
/c
. More importantly, I forgot the default eol is;
! -
yosbel about 10 yearsThanks both for the reply, this one in particular help me solve the problem, then I knew what was the problem with the other one. Thanks @mbroshi and @Magoo!
-
yosbel about 10 yearsNote that to this example works fine it is needed to add the call statement before %KAVDir%.