0

I want to create a list of executables and then create a for loop to taskkill all of the executables in the list. I used this as reference: Create list or arrays in Windows Batch , but I still cannot figure it out.

This is what I tried...

set list = A B C D
for %%a in (%list%) do ( 
taskkill /F %%a
)
Community
  • 1
  • 1
emilk
  • 75
  • 1
  • 3
  • 10

1 Answers1

1

Method 1: setting the list from within the batch file:

@echo off
setlocal enabledelayedexpansion
:assuming you want to kill the calc.exe, paint.exe and notepad.exe processes
set list=calc paint notepad
for %%a in (!list!) do (
set process=%%a
taskkill /f /im !process!.exe
)
endlocal

save the above batch file as method1.bat or include it in your batch file somewhere suitable.

Method 2: have an external list of executables: make a list of the executables you want to terminate, name it list.txt and make sure the list and the batch file are both in the same folder. e.g

List.txt:

notepad.exe

paint.exe

calc.exe

your batch file:

@echo off
::save this batch file as method2.bat or include in your existing batch file
setlocal enabledelayedexpansion
for /f "delims=" %%e in (list.txt) do (
set process=%%e
taskkill /f /im !process!
)
endlocal

hope that helped!

Jahwi
  • 426
  • 3
  • 14
  • What do the lines setlocal enabledelayedexpansion and endlocal do? – emilk Aug 17 '15 at 21:23
  • setlocal: Starts localization of environment variables in a batch file. Localization continues until a matching endlocal command is encountered or the end of the batch file is reached. endlocal: ends the setlocal command. enabledelayedexpansion: enabledelayedexpansion : Enables the delayed environment variable expansion until the matching endlocal command is encountered, regardless of the setting prior to the setlocal command. More here: https://technet.microsoft.com/en-us/library/bb491001.aspx – Jahwi Aug 17 '15 at 21:40
  • Thanks, I remember reading somewhere that if I am doing the for loop in the command line then instead of double %'s as shown in "for %%a" you only need one %. Is this true? – emilk Aug 17 '15 at 22:06