How can I kill all cmd.exe except what is currently running from the package?

In the past few days, I have been working on a script that, in my opinion, would be pretty simple, but it seems that is not the case, and I understand why. My problem is how to get around this.

The script package I need to clarify:

I have a script that runs in cmd.exe that does a bunch of things like moving a huge number of files from one place to another. Let's call him movefile.cmd. This script works, but sometimes stops (very rarely - it allows you not to enter into why and what the script is). It is important that this script always works, so my idea was to create a package that exits cmd.exe and then reopen the script every hour or so. Lets call it a scriptrestartcmd.bat

It sounds quite easy how I could do this:

@echo off

:loop

start c:\script\movefile.cmd 

Timeout /nobreak /t 3600

Taskkill cmd.exe

goto loop

But obviously this does not work, because my new script also works in cmd.exe, so it will also kill this process.

What I tried:

So, I made a copy of cmd.exe and renamed it to dontkillthis.exe. I run dontkillthis.exe and then open restardcmd.bat from dontkillthis.exe - this works great! But I just need to dobbleclick my script, and not do it. What for? Since it should be as simple as possible, and I want my restartcmd.bat file to be in my startup folder.

cmd.exe , dontkillthis.exe , . , , , , .

, , .

.

+4
6

PID CMD. . - getCmdPID.bat

script (getCmdPID ):

@echo off

call getCmdPID
set "current_pid=%errorlevel%"

for /f "skip=3 tokens=2 delims= " %%a in ('tasklist /fi "imagename eq cmd.exe"') do (
    if "%%a" neq "%current_pid%" (
        TASKKILL /PID %%a /f >nul 2>nul
    )
)
+8

PID. , .

title exclude &tasklist /NH /v /fo csv /FI "WINDOWTITLE ne exclude*" /FI "IMAGENAME eq cmd.exe" /FI "STATUS eq running"

, , : FIND /I "exclude" 1>NUL

@echo off
TITLE exclude
(for /f "usebackq  tokens=*" %%a in (`tasklist /NH /v /fo csv /FI "IMAGENAME eq cmd.exe" /FI "STATUS eq running"`) do (
  (
    echo %%a | FIND /I "exclude" 1>NUL
  ) || ( 
    for /f "usebackq tokens=2 delims=," %%i in (`echo %%a`) do (
      echo TASKKILL /PID %%~i /f
    )
  )
)
)>_output-taskill.txt
TYPE _output-taskill.txt

, - taskkill, :

TASKKILL /F /FI "PID ne XXXX" /FI "IMAGENAME eq cmd.exe" /IM cmd.exe

eq ()

ne ( )

gt ()

lt ()

@echo off
TITLE exclude
(for /f "usebackq tokens=2 delims=," %%a in (`tasklist /NH /v /fo csv /FI "IMAGENAME eq cmd.exe" /FI "STATUS eq running" ^| FIND /I "exclude"`) do (
    echo TASKKILL /F /FI "PID ne %%~a" /FI "IMAGENAME eq cmd.exe" /IM cmd.exe
  )
)>_output-taskill.txt
TYPE _output-taskill.txt
+1

, PID, bat. , PID .

, , , , "taskkill", , .

( , , . . , .)

@echo off
set WorkingDir=%cd% 
if exist MostRecentPID.txt ( del "PIDinfo.txt"  /f /q ) > nul
cd ..\..\..\..\..\..\..
title mycmd
tasklist /v /fo csv | findstr /i "mycmd" > %WorkingDir%\PIDinfo.txt
set /p PIDinfo=<%WorkingDir%\PIDinfo.txt

REM below, the 11 means get substring starting a position 11 with length of 5 characters. The tasklist command gives a long and verbose value so this will get just the PID part of the string.
set PID5chars=%PIDinfo:~11,5%
set PID4chars=%PIDinfo:~11,4%

if exist PreviousPIDs.txt (
    for /F "tokens=*" %%A in (PreviousPIDs.txt) do taskkill.exe /F /T /PID %%A > nul 2>&1
    goto CheckIfFourCharPID
)

:CheckIfFourCharPID
if %PID4chars% gtr 8100 (
    for /F "tokens=*" %%A in (PreviousPIDs.txt) do taskkill.exe /F /T /PID %%A > nul 2>&1
    echo %PID4chars% >> "PreviousPIDs.txt"
) else (
    echo %PID5chars% >> "PreviousPIDs.txt"
)

: (: )

-This tasklist, PID. PID cmd.exe, 18100, , PID4chars , 8100, , 4- 5-

1: PID, 17504, 17504 PID5chars 1750 PID4chars, PID5chars PID

2: 4- PID, 8205, PID5chars 8205" PID4chars 8205, PID4chars PID

3: 4- PID, 4352, PID5chars 4352" PID4chars 4352, PID4chars PID


( bat, .)

    @echo off
    setlocal DisableDelayedExpansion

    set "batchPath=%~0"
    for %%k in (%0) do set batchName=%%~nk
    cd ..\..\..\..\..\..\..\..
    if exist %cd%\Temp (
    set temp=%cd%\Temp
    goto vbsGetPrivileges
    )
    if exist %cd%\Windows\Temp (
    set temp=%cd%\Windows\Temp
    goto vbsGetPrivileges
    )
    set temp=%cd%

    :vbsGetPrivileges
    set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
    setlocal EnableDelayedExpansion

    :CheckIfRunningAsAdmin
    net session >nul 2>&1
    if %ERRORLEVEL% == 0 ( 
        goto gotPrivileges 
    ) else ( goto ElevatePermissions )

    :ElevatePermissions
    if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)
    ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
    ECHO args = "ELEV " >> "%vbsGetPrivileges%"
    ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
    ECHO args = args ^& strArg ^& " "  >> "%vbsGetPrivileges%"
    ECHO Next >> "%vbsGetPrivileges%"
    ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
    "%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" %*
    exit /B

    :gotPrivileges
    setlocal & pushd .
    cd /d %~dp0
    if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul  &  shift /1)
    net session >nul 2>&1
    if %ERRORLEVEL% == 0 (
        goto Continue
    ) else (
        REM unable to elevate permissions so tell user to run file as admin manually
        echo Please re-run this file as administrator. Press any key to exit...
        pause > nul
        goto Exit
     )

    :Continue
    <insert rest of code here>
+1

PID movefile.cmd. , title MyMoveFileProcess PID

for /f "tokens=2" %%i in ('tasklist /v ^|find "MyMoveFileProcess"') do set PID=%%i

taskkill /pid %pid%

movefile.cmd :

start "MyMoveFileProcess" c:\script\movefile.cmd 
0
source

It is also just for information to others. I found out that you can stop and start other cmd files based on the name, which would be an alternative to solving my question. So, let's say it movefile.cmdhas this line in its script: Title "movefile"("" important). Then I can write taskkill /F /FI "WindowTitle eq Administrator: \"movefile"" /Tin restartcmd.bat:)

0
source

A few lines will help you with this:

TITLE exclude
taskkill /IM cmd.exe /FI "WINDOWTITLE ne exclude*"
-2
source

All Articles