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.).
set EXE="My Process.exe"
would be acceptable.