1

I'm trying to write one line cmd command that:

  • execute tasklist with PID filter
  • iterate over the output
    • do something if PID found
    • do something else if PID was not found

So I've read How to extract a specific field from output of tasklist on the windows command line and used this answer to construct this command:

for /f "tokens=2 delims=," %F in ('tasklist /nh /fi "PID eq 5284" /fo csv') do if %F NEQ "5284" (@echo no) else  (@echo yes)

I didn't get the expected result. Most likely got the syntax wrong.

Community
  • 1
  • 1
idanshmu
  • 5,061
  • 6
  • 46
  • 92
  • 2
    The loop's command only executes if PID 5284 exists. Try this: `(for /f "tokens=2 delims=," %F in ('tasklist /nh /fo csv /fi "PID eq 5284"') do @echo yes) || @echo no`. – Eryk Sun Mar 29 '15 at 22:15
  • @eryksun That's great. post an answer and I'll accept. 10x – idanshmu Mar 30 '15 at 05:33

1 Answers1

0

Just posting the answer commented by eryksun:

(for /f "tokens=2 delims=," %F in ('tasklist /nh /fo csv /fi "PID eq 5284"') do @echo yes) || @echo no

idanshmu
  • 5,061
  • 6
  • 46
  • 92