Saturday, April 25, 2015

windows 10 - wmic output empty when run from batchfile, but contains info when run from cmd



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?


Answer



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



No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...