1

i have this code : This is a batch file code

@echo off
title Kill all running apps - Bharat Balegere - AgniPulse.com
cd c:\windows\System32
for /f "skip=3 tokens=1" %%i in ('TASKLIST /FI "USERNAME eq %userdomain%\%username%" /FI "STATUS eq running"') do (
if not "%%i"=="svchost.exe" (
if not "%%i"=="explorer.exe" (
if not "%%i"=="cmd.exe" (
if not "%%i"=="tasklist.exe" (
echo.
taskkill /f /im "%%i" 
echo.
)
)
)
)
)
pause

but it will close all of the program

i try to edit it and add something like this

if not "%%i"=="notepad.exe" (
but its not working anyway and when i click on batch file its nothing so i don't know how to edit this and put some except on it! thanks in advance
Amin
  • 27
  • 8
  • I tested your code and it worked for me. Did not kill any of the executables you have listed in your `IF` comparisons. – Squashman Sep 07 '17 at 01:29
  • 1
    You should consider using `if /I not ...` to ignore case. – kodybrown Sep 07 '17 at 02:47
  • Did you remember to add a matching close parenthesis (`)`) line? Either way, [Hackoo's answer](https://stackoverflow.com/a/46097178/2096401) is much cleaner (though both are potentially dangerous). – TripeHound Sep 07 '17 at 13:09

1 Answers1

0

You can give a try for this batch file with a whitelist for killing process

Of course you can modify your whitelist variable as you need :

@echo off
Title Kill all running apps - Bharat Balegere - AgniPulse.com
set "whitelist=avast mbam dllhost conhost" 
Set "whitelist=%whitelist% taskeng skype"
Set "whitelist=%whitelist% firefox explorer cmd"
Set "whitelist=%whitelist% chrome tasklist taskmgr notepad"

@For /f "skip=3 tokens=1" %%i in (
'TASKLIST /FI "USERNAME eq %userdomain%\%username%" /FI "STATUS eq running" ^| findstr /VI "%whitelist%"'
) Do ( 
    echo Taskkill /IM "%%i" /F
)
pause & exit

You can of course remove the echo command if that you want this list to be killed !


EDIT : And here is another batch with whitelist using WMIC

@echo off
Title Process killer with a white list
Mode con cols=50 lines=40 & color 9B
setlocal
Set "White_Process_List=%~dpn0_White_Process_List.txt"
set "Ignoring_Process_List=%~dpn0_Ignoring_Process_List.txt"
set "Terimnated_Process_List=%~dpn0_Terimnated_List.txt"
If exist "%Ignoring_Process_List%" Del "%Ignoring_Process_List%"
If exist "%Terimnated_Process_List%" Del "%Terimnated_Process_List%"

:Whitelist
set "whitelist=Microsoft Mozilla Google Avast Panda ESET Kaspersky Avira AVG Bitdefender Malwarebytes Norton McAfee GAS IBM Skype"
If not exist "%White_Process_List%" echo %whitelist% > "%White_Process_List%" 
Set /p whitelist=<"%White_Process_List%"

:Analyze
for /f "tokens=2 delims=," %%I in (
    'wmic process get executablepath^,status /format:csv ^| find "\"'
) do (
    set "proc=%%~I"
    setlocal enabledelayedexpansion
    wmic datafile where "name='!proc:\=\\!'" get manufacturer 2>nul | findstr /i "%whitelist%" >nul && (
        ( echo Ignoring : %%~nxI & echo "%%~I" 
         echo --------------------------------------- )>>!Ignoring_Process_List! 2>&1
    ) || (
        ( echo Terminating: %%~nxI )>con
        ( echo Terminating : %%~nxI & echo "%%~I" 
        Taskkill /im "%%~nxI" /f /t 
        echo --------------------------------------- )>>!Terimnated_Process_List! 2>&1
    )
    endlocal 
)
Timeout /T 2 /nobreak>nul
Hackoo
  • 18,337
  • 3
  • 40
  • 70