2

I am trying to include in a batch file a check to see if a java process is already running before attempting to start it.

I have the following working command:

FOR /F "tokens=*" %g IN ('wmic PROCESS where "name like '%java%' and CommandLine like '%surveyEngine%'" get ProcessId') DO (SET var=%g)
ECHO %var%

However, the var variable is NOT being set correctly.

When I run the initial wmic command I get three lines returned;

 ) Users\gavin>(SET var=ProcessId

 ) Users\gavin>(SET var=4964

 ) Users\gavin>(SET var=

and if I attempt to ECHO %var% I get ECHO is on and not a value.

Can anyone see any tweaks to my command that will allow me to capture the second line?

I am not "stuck on" using wmic... I just care about is this process running, or not.

2 Answers 2

0

The var variable is NOT being set correctly.

Inside a batch file you needed to use 2 %s instead of 1.

The operation of the FOR command can be summarised as...

  • Take a set of data
  • Make a FOR Parameter %%G equal to some part of that data
  • Perform a command (optionally using the parameter as part of the command).
  • Repeat for each item of data

If you are using the FOR command at the command line rather than in a batch program, use just one percent sign: %G instead of %%G.

Source: For - Looping commands - Windows CMD - SS64.com

To capture only the 2nd line:

rem skip 1st line
FOR /F "tokens=* skip=1" %%g IN ('wmic PROCESS where "name like '%java%' and CommandLine like '%surveyEngine%'" get ProcessId') DO (
    SET var=%%g
    rem finished
    goto done
    )
done:
ECHO %var%

Further Reading

1
  • I had to double up the % in the WHERE clause to get it working - but otherwise - thanks for your help! Commented Apr 18, 2023 at 3:43
0
@echo off

for /f useback^tokens^=4^delims^=^<^> %%g in (
`%__AppDir__%wbem\wmic.exe Process where "Name like '%java%' and CommandLine like '%surveyEngine%'" get ProcessId /format:xml ^| find "VALUE"
`)do set "_var=%%~g"

echo; %_var%

However, the var variable is NOT being set correctly.
That's not right to say, in fact the variable takes on a value in the
1st and 2nd loop of the for output, but in the last one it removes the variable.

 SET var=ProcessId
 SET var=4964
 SET var=    // remove variable // 

The output (encodind) of wmic.exe doesn't help and generates line break when using in the simple form (get MyPizza). After a few tries with the different optional output formats (via command complement), I started to make use of /format:xml with ^| find "VALUE" to get a line of output and not have the extra spaces and/or newlines.

When copying a command|clip output and pasting it into an edit, notice the additional lines:

> wmic PROCESS where "name like '%cmd.exe%' and CommandLine like '%windows%'" get commandline|clip
  • Output pasted into a text editor:

enter image description here

I suggest that you consult this question:
WMIC command in batch outputting non UTF-8 text files

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .