I'm trying to get the output of wmic process call create
so I can get the ProcessId
of the newly created process. If I just run:
wmic process call create "notepad.exe a.txt","d:\"
It works just fine (it opens the file a.txt
under folder d:\
with notepad
). Now, if I try:
for /f "usebackq delims==; tokens=1,2" %i in (`wmic process call create "notepad.exe a.txt","d:\"^|findstr ProcessId`) do @echo pid = %j
It doesn't, and shows me the error:
Formato incorrecto. Sugerencia: <lista_parámetros> = <parámetro> [, <lista_parámetros>].
I don't really know what's going on here, can anyone explain this to me or if this is possible at all?
NOTE: Other commands work fine. For instance, if I run:
for /f "usebackq" %i in (`wmic process where "executablepath like '%%notepad.exe'" get ProcessId /value^|findstr ProcessId`) do @echo OUTPUT: %i
It gives the expected output, which in this case is:
OUTPUT: ProcessId=2084
Thanks!
There is no need to initiate another
cmd
instance. You could do it as follows:You need to escape the
,
like^,
, so thecmd
instance initiated by thefor /F
loop receives it literally; otherwise, it becomes most likely replaced by a SPACE, because tocmd
a comma is nothing but a token separator just like the SPACE.When the given text file
a.txt
does not exist, the above code does not open anotepad.exe
instance, even if executed without surroundingfor /F
loop:I guess this behaviour comes from the pipe (
|
), because it initiates newcmd
instances for either side; I believenotepad.exe
(or any other application) is run in a kind of hidden mode where no user interaction is required (for the application not to await input in the background to no avail).Therefore I recommend to use a variant without need of a pipe, like this:
The outer
for /F
loop uses the default delimiters TAB and SPACE in order to remove the leading TAB in front of the keywordProcessId
, the inner loop contains anif
condition to extract the correct line (instead offind
) and splits off everything but the process ID number.I just found how can it be done. Due to the command was working only directly in the command prompt I decided to use
cmd /c "..."
, it's as follows:Now the only thing is that the
PID
needs to be trimed out of spaces, but that something else. Sorry for the inconvinience.