Wie würde ich sehen, ob eine bestimmte Batch-Datei läuft und die PID erhält?

494
Mark Deven

Es ist ein häufiges Problem, bei dem ich häufig gestoßen bin, wo ich sehen möchte, ob eine andere Batchdatei ausgeführt wird oder nicht. Das ist ziemlich einfach, aber die PID zu bekommen ist schwer. Aus diesem Grund habe ich mein eigenes Skript geschrieben, das eine Menge Dinge tut, die ich als Antwort für diejenigen posten werde, die es brauchen. Wenn noch jemand etwas verbessern will, bitte!

1

1 Antwort auf die Frage

1
Mark Deven

Hinweis: Das Skript kann von jedermann frei verwendet werden. Bitte hinterlassen Sie das Guthaben, wo das Guthaben fällig ist. Vielen Dank!

Hier einige der Möglichkeiten, die das Skript ausführen kann:

  1. Es kann Ihnen zeigen, welche CMD.exe-Fenster (Hintergrund oder Vordergrund) mit einer ansprechenden Benutzeroberfläche ausgeführt werden.
  2. Es kann ständig aktualisiert werden, um die Änderungen im Titel einer Instanz und die Speicherauslastung anzuzeigen.
  3. Es kann nur aktualisiert werden, wenn ein neues Fenster angezeigt wird (weniger störend).

  4. Es kann vor dem Beenden eine Pause einlegen, was für die Verwendung mit Run nützlich ist.

  5. Sie kann aus einer Batchdatei heraus ausgeführt werden, um nach einer Instanz mit einem bestimmten Titel zu suchen und den Fehlerspiegel auf die PID des Fensters festzulegen.

Hier ist ein Screenshot des Hilfebildschirms (/?): Help Screen

Hier ist ein Screenshot des Displays: cmds default

BEARBEITEN: Das Programm ist jetzt dank der Eingabe von DavidPostill voll funktionsfähig (Danke)

Das Skript:

@echo off set oldnum=NO setlocal EnableDelayedExpansion pushd "%TEMP%" for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do ( set "DEL=%%a" ) rem Prepare a file "X" with only one dot <nul > X set /p ".=."   if /i "%1"=="/TS" goto ts if /i "%1"=="/S" tasklist /fi "imagename eq cmd.exe" /fo list /v & exit /b if "%1"=="/?" goto help goto nxt :nxt ::Setlocal EnableDelayedExpansion :: ===================== Window Title ================= set num=0 tasklist /fi "imagename eq cmd.exe" /fo list /v | find /I "Window Title:" >System for /F "tokens=*" %%A in (System) do ( set /a num+=1 set Title!num!=%%A set totalnum=!num! )    :: ===================== Memory ======================= set num=0 tasklist /fi "imagename eq cmd.exe" /fo list /v | find /I "Mem Usage:" >System for /F "tokens=*" %%A in (System) do ( set /a num+=1 set Mem!num!=%%A )  :: ===================== PID ======================== set num=0 tasklist /fi "imagename eq cmd.exe" /fo list /v | find /I "PID:" >System for /F "tokens=*" %%A in (System) do ( set /a num+=1 set PID!num!=%%A ) if "%1"=="/w" goto lloop if "%1"=="/W" goto lloop :Display echo [92mCMDS by IT Command (use /? for help) %totalnum% Windows Open[0m echo ===================================================================================== set num=0 setlocal EnableDelayedExpansion :tpds set /a num+=1 if %num% LSS 10 call :Colorecho21 08 "%num% ] " if %num% GTR 9 call :Colorecho21 08 "%num%] "  set str=!PID%num%! set "result=%str::=" & set "result=%" set result=%result: =% call :Colorecho21 0b "PID: %result% " if %result% LSS 10000 call :Colorecho21 0f " " call :Colorecho21 0e "!Mem%num%! " echo [92m!Title%num%![0m if %num%==%totalnum% goto stops11 goto tpds :stops11 echo ===================================================================================== endlocal if "%1"=="/p" pause if "%1"=="/P" pause if "%2"=="/P" pause if "%2"=="/p" pause if "%3"=="/P" pause if "%3"=="/p" pause  if "%1"=="/l" echo Press any key to continue or CTRL+C to quit . . . & pause>nul & cls & goto nxt if "%1"=="/L" echo Press any key to continue or CTRL+C to quit . . . & pause>nul & cls & goto nxt if "%2"=="/l" echo Press any key to continue or CTRL+C to quit . . . & pause>nul & cls & goto nxt if "%2"=="/L" echo Press any key to continue or CTRL+C to quit . . . & pause>nul & cls & goto nxt if "%3"=="/l" echo Press any key to continue or CTRL+C to quit . . . & pause>nul & cls & goto nxt if "%3"=="/L" echo Press any key to continue or CTRL+C to quit . . . & pause>nul & cls & goto nxt goto exit   :Displayl1 echo [92mCMDS by IT Command (use /? for help) %totalnum% Windows Open[0m echo ===================================================================================== set num=0 setlocal EnableDelayedExpansion :tpdsl1 set /a num+=1 if %num% LSS 10 call :Colorecho21 08 "%num% ] " if %num% GTR 9 call :Colorecho21 08 "%num%] "  set str=!PID%num%! set "result=%str::=" & set "result=%" set result=%result: =% call :Colorecho21 0b "PID: %result% " if %result% LSS 10000 call :Colorecho21 0f " " call :Colorecho21 0e "!Mem%num%! " echo [92m!Title%num%![0m if %num%==%totalnum% goto stops11l11 goto tpdsl1 :stops11l11 echo ===================================================================================== :stops11l1 ( endlocal set oldnum=%num% ) timeout /t 3 >nul Setlocal EnableDelayedExpansion goto nxt   :lloop if %oldnum%==NOT goto displayl1 if %num%==%oldnum% goto stops11l1 cls goto displayl1      :help call :Colorecho21 0f "CMDS Command Prompt Window Lister by IT Command" echo. echo. echo CMDS [/S] [/P] [/L] [/W] [/TS String] echo. echo /S Displays the simple but high information version (fast) echo /P Pauses Before Exiting. Usefull if using from Run. echo /L Pauses and refreshes on press of key. Use CTRL+C to quit. echo /W Refreshes only when a new cmd instance starts (new PID). echo Note: This will not refresh if an old window closes echo and a new one opens at the same time. echo /TS Use within a batch file to search for a Window Title echo String The Window Title to search for with /TS  echo. echo. echo with /TS the errorlevel will be set to 1 if the title was not found. echo If it is found, the errorlevel will be set to the PID of the cmd instance. echo. pause echo. echo Example: echo. echo CMDS /TS "My Window" echo. echo The Above Command Will set the errorlevel to the PID of the cmd instance echo with the title "My Window" (set with the title command). If the instance echo is not found (there is no running window) the errorlevel will be 1. echo if the Syntax was incorrect, errorlevel will be set to 2. echo. echo. call :Colorecho21 07 " Created by Lucas Elliott with IT Command" call :Colorecho21 0b " www.ITCommand.tech" echo. echo. goto exit  :ts set num=0 tasklist /fi "imagename eq cmd.exe" /fo list /v | find /I "Window Title:" >System for /F "tokens=*" %%A in (System) do ( set /a num+=1 set Title!num!=%%A set totalnum=!num! )    :: ===================== Memory ======================= set num=0 tasklist /fi "imagename eq cmd.exe" /fo list /v | find /I "Mem Usage:" >System for /F "tokens=*" %%A in (System) do ( set /a num+=1 set Mem!num!=%%A )  :: ===================== PID ======================== set num=0 tasklist /fi "imagename eq cmd.exe" /fo list /v | find /I "PID:" >System for /F "tokens=*" %%A in (System) do ( set /a num+=1 set PID!num!=%%A ) setlocal EnabledelayedExpansion set num=0 :tsloop set /a num+=1 if "%~2"=="" exit /b 2 if "!Title%num%!"=="Window Title: %~2" goto isrite if %num%==%totalnum% goto nonets goto tsloop :isrite ::window was found set str=!PID%num%! set "result=%str::=" & set "result=%" set result=%result: =% popd exit /b %result%  :nonets popd endlocal exit /b 1  :colorEcho21 set "param=^%~2" ! set "param=!param:"=\"!" findstr /p /A:%1 "." "!param!\..\X" nul <nul set /p ".=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%" exit /b   :exit popd endlocal exit /b 

HINWEIS: Zum jetzigen Zeitpunkt ist die neueste Version verfügbar. Die zuletzt aktualisierte Version finden Sie hier: https://github.com/ITCMD/CMDS

Es gibt auch eine Funktionsversion, die Sie unter dieser URL in Ihren Code einfügen können.

Sie müssen Pushd und Popd verwenden. DavidPostill vor 5 Jahren 1
Eigentlich scheint @DavidPostill nicht zu funktionieren. Ich habe das Skript bearbeitet, habe ich es falsch gemacht? Mark Deven vor 5 Jahren 0
Wenn es beendet wird, befindet es sich im temporären Verzeichnis. Mark Deven vor 5 Jahren 0
Sieht für mich ok aus. Kein offensichtlicher Grund, warum es nicht funktionieren sollte ... DavidPostill vor 5 Jahren 0
Wenn Sie es ausprobieren, wird es im temporären Ordner abgelegt. Mark Deven vor 5 Jahren 0
Ihr Code ist fehlerhaft. Achten Sie auf Ihre verzögerten Erweiterungen. Folgendes funktioniert: `setlocal enabledelayedexpansion pushd test dir popd endlocal ' DavidPostill vor 5 Jahren 0
Sie haben auch mindestens einen "Ausgang" ohne vorangestelltes "Popd" DavidPostill vor 5 Jahren 0
Nun, ich habe 'pushd setlocal enabledelayedexpansion test dir popd endlocal' ausprobiert und es ist fehlgeschlagen. Ich vermute, Pushd / Popd muss entweder außerhalb oder innerhalb von // // Setlocal-Endlocal sein DavidPostill vor 5 Jahren 1
Das hat funktioniert. und wo sind diese Ausgänge? Ich habe `goto exit`s, aber ich kann mehr als 3 Mal finden, wenn ich aussteige. Mark Deven vor 5 Jahren 0
In `: colorEcho21` haben Sie` exit / b` ohne vorhergehendes `popd` DavidPostill vor 5 Jahren 0
Das beendet einfach den Anruf, nicht das Programm. Mark Deven vor 5 Jahren 0