Reputation: 1
I can get a proper output wuth the below command in the command prompt,
tasklist /fi "imagename ne siebde*" /fi "imagename eq sieb*" /svc | find "gtwyns"
.
But if i want to use this condition in batch files i have to do this with the below command.
for /f "tokens=2 delims= " %%a in ('tasklist /fi "imagename ne siebde*" /fi "imagename eq sieb*" /svc ^| find "gtwyns")
I need to understand the function of ^ character and how does it work actually?
I also wanna know does it open a new cmd when wi use a pipe in batch script?
Upvotes: 0
Views: 159
Reputation: 30258
Read FOR /F
: loop command against the results of another command syntax:
FOR /F ["options"] %%parameter IN ('command_to_process') DO command … command_to_process : The output of the 'command_to_process' is passed into the FOR parameter.
… The
command_to_process
can be almost any internal or external command.
Almost any internal or external command (but the only command).
Now, read redirection syntax:
commandA | commandB
Pipe the output fromcommandA
intocommandB
For instance, in wrong for /F "delims=" %%a in ('dir /B | sort /R') do echo %%~a
with unescaped |
pipe:
commandA
evaluates to for /F "delims=" %%a in ('dir /B
andcommandB
evaluates to sort /R') do echo %%~a
although separate dir /B | sort /R
is right command.Hence, we need to escape (all) &
, |
, <
, >
redirection characters and (sometimes) "
double quotes as follows (both ways are equivalent parsing dir /B "*.vbs" 2>NUL | sort /R
command):
for /F "delims=" %%a in (' dir /B "*.vbs" 2^>NUL ^| sort /R ') do echo %%~a
for /F "delims=" %%a in ('"dir /B ""*.vbs"" 2>NUL | sort /R"') do echo %%~a
Therefore, next two loops should work the same way:
for /f "tokens=2 delims= " %%a in ('
tasklist /fi "imagename ne siebde*" /fi "imagename eq sieb*" /svc ^| find "gtwyns"
') do echo set pid_ns=%%a
and
for /f "tokens=2 delims= " %%a in ('
"tasklist /fi ""imagename ne siebde*"" /fi ""imagename eq sieb*"" /svc | find ""gtwyns"""
') do echo set pid_ns=%%a
Upvotes: 2