1

I am trying to parse the output of a command in a command line script. As a start i have to first capture the output of the command in a variable ( i guess) . So here is what i have written so far:

FOR /F "tokens=2* delims=    " %%A IN ('wmic process where(name="javaw.exe") get commandline') DO SET VAR=%%B
echo %VAR%

where wmic process where(name="javaw.exe") get commandline is the command i want to execute and save the output. But i get the following error upon executing this.

get was unexpected at this time

Any ideas why the error. And how do i proceed after storing the output in a variable. I am looking for a particular word say "XYZ" in the output and if that word is there i want to display a windows pop-up.

Thanks in advance for the help.

1
  • 1
    It's a batch script or it's executed at the cmd line, but command line scripts doesn't exists Commented Nov 9, 2012 at 9:42

2 Answers 2

3

The reason it is giving you an error is because the first ) in the for loop is effectively ending the loop, then the get command is processed separately, which isn't recognised.

You need to use the caret ^ to escape the brackets and the =.

FOR /F "tokens=2* delims=    " %%A IN ('wmic process where^(name^="javaw.exe"^) get commandline') DO SET VAR=%%B

Hope this helps

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

4 Comments

Nope. Now i get "B was unexpected at this time"!!
Are you running from cmd line or batch file?
Then you need to use single %'s in the for loop, you use double %% for batch files. %A and %B, rather than %%A and %%B
+1 - but actually the 2nd ( does not cause problems and does not need to be escaped. It is the first ) that is causing the problem, and it needs to be escaped as you have shown.
2

You need to change the syntax, but even then you will not get the desired results, as wmic normally responds with more than one line.

setlocal EnableDelayedExpansion
FOR /F "tokens=* delims=" %%A IN (
    '"wmic process where(name="javaw.exe") get commandline"') DO (
     set "line=%%A"
     if "!line:XYZ=!" NEQ "!line!" (
        echo The line contains XYZ
     )
)

And setting delims to space will split the line at the first space, even if this is in the path of javaw.exe.

3 Comments

can i search for a string directly in the output without saving it in a variable?
I added a solution which search for XYZ in each line of the WMIC output
ok. Suppose i change my command to "wmic process where (name="javaw.exe") get commandline | find "XYZ". Then there would be only one result in the output if any[i know]. Now what can i do? I mean how to check if the length of the output and so on if the length is more than zero display a popup..

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.