EmpollonDeFisica
EmpollonDeFisica

Reputation: 5

How to store output of .bat FIND in a variable

I'm writing a batch file to find out if certain programs are currently running. Here is my code so far:

for %%x in ("notepad++.exe" "eclipse.exe") do ( 
tasklist /FI "IMAGENAME eq %%x" | find /I /C %%x
)

What I would like to be able to do is store the result from the "tasklist" line (which should be a number that is greater than or equal to 0) in a variable.

I guess I am really asking: is it possible to capture this output; and, if so, how?

Upvotes: 0

Views: 199

Answers (1)

Jon
Jon

Reputation: 437444

With FOR /F:

for %%x in ("notepad++.exe" "eclipse.exe") do ( 
  for /f %%c in ('tasklist /FI "IMAGENAME eq %%x" ^| find /I /C %%x') do echo %%c
)

Put the command whose output you want to capture inside single quotes and escape any characters with special meaning in the shell, such as the pipe, with the shell escape character ^.

Don't forget to look at FOR /? for additional options when using FOR /F.

Upvotes: 1

Related Questions