How to kill a process with space in its name using a loop for/do in batch ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

How to kill a process with space in its name using a loop for/do in batch ?

#1 Post by Hackoo » 23 Feb 2016 04:41

Hi :wink:
I have this batch for killing some process !

Code: Select all

@echo off
set process=Program with space.exe,winword.exe,Opera.exe,mshta.exe,chrome.exe,calc.exe,skype.exe,iexplore.exe
set Tmp=Tmp.txt
set LogFile=ProcessKillerLog.txt
If Exist %Tmp% Del %Tmp%
If Exist %LogFile% Del %LogFile%
@echo on
For %%a in (%process%) Do Call :KillMyProcess "%%a" %Tmp%
Cmd /U /C Type %Tmp% > %LogFile%
If Exist %Tmp% Del %Tmp%
Exit /b

:KillMyProcess
Taskkill /IM "%1" /F /T >>%2 2>&1

So my aim is to kill a process with space in its name like "Program with space.exe" what should i modify ?
Thank you !

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to kill a process with space in its name using a loop for/do in batch ?

#2 Post by foxidrive » 23 Feb 2016 05:42

Any good?

Code: Select all

set process="Program with space.exe","winword.exe","Opera.exe","mshta.exe","chrome.exe","calc.exe","skype.exe","iexplore.exe"

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: How to kill a process with space in its name using a loop for/do in batch ?

#3 Post by Hackoo » 23 Feb 2016 06:02

foxidrive wrote:Any good?

Code: Select all

set process="Program with space.exe","winword.exe","Opera.exe","mshta.exe","chrome.exe","calc.exe","skype.exe","iexplore.exe"

Thank you foxidrive :wink: your solution :idea: works :lol:
And this another answer here on stackoverflow

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to kill a process with space in its name using a loop for/do in batch ?

#4 Post by foxidrive » 23 Feb 2016 06:10

Ta. The code you showed Jeb is different to here - you need the ~ in the "%%~a" as well as the "%~1"

Code: Select all

For %%a in (%process%) Do Call :KillMyProcess "%%~a" %Tmp%

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: How to kill a process with space in its name using a loop for/do in batch ?

#5 Post by Hackoo » 23 Feb 2016 06:20

foxidrive wrote:Ta. The code you showed Jeb is different to here - you need the ~ in the "%%~a" as well as the "%~1"

Code: Select all

For %%a in (%process%) Do Call :KillMyProcess "%%~a" %Tmp%


Yes ! I used it like this :

Code: Select all

@echo off
set process="Program with space.exe" winword.exe Opera.exe mshta.exe chrome.exe calc.exe skype.exe
set Tmp=Tmp.txt
set LogFile=ProcessKillerLog.txt
If Exist %Tmp% Del %Tmp%
If Exist %LogFile% Del %LogFile%
For %%a in (%process%) Do Call :KillMyProcess %%a %Tmp%
Cmd /U /C Type %Tmp% > %LogFile%
If Exist %Tmp% Del %Tmp%
Start "" %LogFile%
Exit /b

:KillMyProcess
Taskkill /IM  "%~1" /F /T >>%2 2>&1

Post Reply