1

I have this wmic command which finds out the PID of my process and works fine from cmd, but refuses to produce output when executed in a bat-file:

wmic /output:process.pid process where (Name like '%java%' and commandLine like '%part-of-the-path%') get ProcessId

When I run it from cmd, it produces a file containing

ProcessId
19352

The info is followed by two blank lines. When I run it from a batch-file, it only produces a file with two blank lines.

I tried /output:filename, > filename and | more > filename, all with the same result.

Why is this? How to make the command produce output from the batch?

  • 1
    In batch file, are you replacing % with %%? %jave% on command prompt must be %%java%% in batch file. – Sandeep Jun 18 '18 at 12:36
  • %java% is not set beforehand. Like SQL, wmic uses this as an asterisk matching any text. I don't want batch to interpret the string, but just give it to wmic as is. The solution from @Sandeep seems to work. – sinned Jun 18 '18 at 12:48
2

Just like most of programming languages, a batch file also has escape characters. % sign used in command line is an escape character in batch file and give the command a different meaning. It is actually looking for a variable named %java%.

To resolve this problem, use %% instead of % in batch file. To see more details and information on other escape character look here.

Following command should work in batch file.

wmic /output:process.pid process where (Name like '%%java%%' and commandLine like '%%part-of-the-path%%') get ProcessId

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.