1

I am trying to detect a running process on Windows 7 using a batch file. Processes without any spaces work just fine - e.g. notepad.exe. I cannot understand why it doesn't work and would really appreciate any help.

CODE

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set "EXE=My Process.exe"

FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto ProcessFound 

goto ProcessNotFound

:ProcessFound
echo 1
goto END

:ProcessNotFound
echo 0
goto END

:END

EXPECTED RESULT

C:\test.bat
1

ACTUAL RESULT

C:\test.bat
'Process.exe' is not recognized as an internal or external command,
operable program or batch file.
0
2
  • Your syntax is wrong. set EXE="My Process.exe" would be acceptable.
    – Ramhound
    Mar 12, 2015 at 17:05
  • set EXE="My Process.exe" doesn't work either. Error returned is now:- ERROR: Invalid argument/option - 'Process.exe'
    – Reado
    Mar 12, 2015 at 17:36

3 Answers 3

1

There is one major problem with your code, and it has nothing to do with spaces in the process name.

When you run tasklist /NH each line returned is more than just the name of the process:

C:\temp>tasklist /nh

System Idle Process              0 Services                   0         24 K
System                           4 Services                   0      2,200 K
smss.exe                       400 Services                   0      1,232 K

So you're attempting to take a line like:

System Idle Process 0 Services 0 24 K (which is what %%x would hold), and see if it's equal to the provided process name (ie: System Idle Process).

Due to the extra info provided on each line it will never be equal, so you need to find a way to acquire a list of (or extract further) only the process names.

Also:

set "EXE=My Process.exe" doesn't need the quotes. It knows that everything on the line after the equals sign is the value to assign to the variable named on the left side. So set EXE=My Process.exe will work fine, even with the spaces.

Including the quotes around the value part (as suggested in comments) is the usual way to delimit strings, but since Set is not type-based (it doesn't know a string from a number from a triangle, etc.) it will include the quotes. So set Filename="My Name" means Filename's value is now "My Name", including the quotes. This can get confusing/break things when trying to use it later (in between other quotes in other commands, etc.).

3
  • 1
    I only suggested the strings because my own batch script didn't contain them but also didn't have spaces. But you are indeed right.
    – Ramhound
    Mar 12, 2015 at 17:42
  • @Ramhound Honestly, wrapping the value in quotes was my first thought as well, I had to run my test batch a couple times until the memories came back to remind me that it keeps the quotes in the value. :) Mar 12, 2015 at 17:45
  • Firstly it's not my code; I'm a complete novice when it comes to this sort of language. I did was you suggested; using EXE=My Process.exe still results in the same error. It's trying to run "Process.exe" as a command. Why is that?
    – Reado
    Mar 12, 2015 at 18:00
0

I found some different code which does the job:

@echo off
tasklist /nh /fi "imagename eq My Process.exe" | find /i "My Process.exe" >nul && (
echo 1
) || (
echo 0
)
0

I stumbled across this and the answers did not help my case, so I found a workaround for anyone stumbling across a similar case.

as other answers before me mentioned set "EXE=My Process.exe" works fine without the quotations.

the bigger problem comes with the for loop, if you run it like you did FOR /F %%x then you notice that %%x contains only the first word of the process, in this example My from My Process.exe.

To fix that you can use the token and delims flags in the FOR /F loop command. By adding the first 2 strings as tokens you get 2 variables My and Process.exe, then you combine them and compare to the original EXE var.

Should look something like this:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set EXE=My Process.exe

FOR /F "tokens=1,2 delims= " %%G IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF "%%G %%H" == "%EXE%" goto ProcessFound 

goto ProcessNotFound

:ProcessFound
echo 1
goto END

:ProcessNotFound
echo 0
goto END

:END

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .