0

I have this command that scan a file and returns a summary.

For example on running this command

omsCmdLineUtil.exe process C:\test.exe Default

the result output is:

Ticket:[ 2214271306 ]

Process Details
---------------


File: [ C:\test.exe ]
   MD5: [ D41D8CD98F00B204E9800998ECF8427E ]
   SHA1: [ DA39A3EE5E6B4B0D3255BFEF95601890AFD80709 ]
   SHA256: [ E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 ]
   File Size: [ 0 bytes ]
   File Type  Category: [ O ]
   File Type: [ - ]
   File Type Description: [ empty ]

[ Clean ] Ahnlab scan engine [ 1 ms ]
[ Clean ] ClamAV scan engine [ 1 ms ]
[ Clean ] BitDefender scan engine [ 1 ms ]
[ Clean ] Avira scan engine [ 1 ms ]
[ Clean ] Quick Heal scan engine [ 1 ms ]
[ Clean ] ThreatTrack scan engine [ 1 ms ]
[ Clean ] ESET scan engine [ 1 ms ]
[ Clean ] Total Defense scan engine [ 1 ms ]

Scan Completion
---------------
        [ Clean ]
        Ticket: [ 2214271306 ]
        File path: C:\test.exe
        Scan time: 1 ms [12/20/2015 13:00:06:791]


Process Completion
------------------
        Ticket: [ 2214271306 ]
        User agent: Default
        Profile: Default
        Result: [ Allowed ]
        File processed: C:\test.exe

I want to create a batch file that parses this result by searching for the output line Result:, check if it's [ Allowed ] or [ Blocked ] and return 0 for allowed and 1 for blocked.

I tried something like this, but its not really working:

omsCmdLineUtil.exe process C:\test.exe Default | set ts = findstr /C:"Result: [ Allowed ]"

if %ts% == "Result: [ Allowed ]" return 0
else return 1

Which modification on code is necessary to get the expected result?

2
  • 1
    basically, because there is no variable %ts%. You produced one with the Name %ts %. Be carefull with spaces in Batch. Commented Dec 20, 2015 at 12:25
  • Also, the variable you created is temporary because each side of the pipe is run in a child process. The variable disappears once the pipe is closed. Commented Dec 20, 2015 at 14:23

1 Answer 1

1

there is no return in Batch. I think, you want exit /b <errorlevel>

omsCmdLineUtil.exe process C:\test.exe Default | find "Result: [ Allowed ]" >nul && Exit /b 0 || Exit /b 1

Instead of Exit 0 you can of Course also use set ts=0 and use that. Or use echo instead.

Some explanations:

>nul redirects the output to nirvana, keeping your screen clean.

&& acts as "If previous command was successfull, then..." (string was found)

|| acts as "if previous command was not successfull, then...` (string was not found)

I prefer using find when possible because of it's simpler syntax, but of course findstr /C:"Result: [ Allowed ]"will also work

Sign up to request clarification or add additional context in comments.

5 Comments

exit will also terminate the cmd instance the batch file is running in, whereas exit /B only terminates the batch file itself...
FIND already returns ERRORLEVEL of 0 for found and 1 for not found, so the conditional EXIT /B statements are redundant. You could simply issue a single EXIT /B (without specifying ERRORLEVEL) and the FIND result will be propagated.
@dbenham yes, but better be sure: something like ...||(timeout 5&exit /b) may ruin it.
thank you for all the great answers, i have another sub-question. what if i want to find two lines in the output . is that also easily possible ? thanks
findstr is able to do that. See findstr /?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.