138701

Aktives Fenster ermitteln

Hallo zusammen,

Wie kann ich mit Batch ermitteln, welches Programm gerade im Vordergrund läuft?
Auf Facebook teilen
Auf X (Twitter) teilen
Auf Reddit teilen
Auf Linkedin teilen

Content-ID: 437276

Url: https://administrator.de/forum/aktives-fenster-ermitteln-437276.html

Ausgedruckt am: 12.05.2025 um 16:05 Uhr

rubberman
rubberman 05.04.2019 um 22:34:34 Uhr
Goto Top
Das ist sehr einfache. Sobald du das Batchscript ausführst, ist das Batchfenster im Vordergrund. Das Programm ist also immer cmd.exe.

Steffen
139374
139374 05.04.2019 aktualisiert um 22:59:38 Uhr
Goto Top
Ein Freitag der Extraklasse 😂😂😂

Was so manch ein perverser mit Batch alles anstellen will 🙃🙃🙃*

An den TO: Gehe 20 Schritte vor und biege hier ab ---v

https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser- ...

Gehe nicht über Los, ziehe keine 100 Mäuse ein und ziehe sofort im Zuchthaus ein .... 🚑🤕
rubberman
Lösung rubberman 05.04.2019 aktualisiert um 23:53:52 Uhr
Goto Top
Apropos timeout. Damit könnte man zumindest testen was passiert wenn das Batchfenster den Fokus verliert. Ich lass mal noch ein bisschen Batch dabei. Eigentlich Get-Process in PS ...
@echo off &setlocal

set foregroundproc=powershell -nop -ep Bypass -c "$c=Add-Type -Name pInv -PassThru -MemberDefinition '[DllImport(\"user32.dll\")]public static extern IntPtr GetForegroundWindow();[DllImport(\"user32.dll\")]public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint processId);';$procid=[UInt32]0;$null=$c::GetWindowThreadProcessId($c::GetForegroundWindow(),[ref]$procid);exit $procid;"  

setlocal EnableDelayedExpansion
for /l %%i in () do (
  %foregroundproc%
  echo PID=!errorlevel!
  for /f delims^=^" %%i in ('tasklist /nh /fi "pid eq !errorlevel!" /fo csv') do echo %%i  
  >nul timeout /t 1 /nobreak
)
Steffen
138701
138701 06.04.2019 aktualisiert um 18:40:50 Uhr
Goto Top
@rubberman

Und wie kann ich es statt als Schleife nur einmal abfragen?

Oder geht das nur so:

for /l %%i in () do (
  %foregroundproc%
  echo PID=!errorlevel!
  for /f delims^=^" %%i in ('tasklist /nh /fi "pid eq !errorlevel!" /fo csv') do echo %%i & goto jump  
)

:jump
rubberman
Lösung rubberman 06.04.2019 aktualisiert um 19:58:22 Uhr
Goto Top
Zitat von @138701:
Und wie kann ich es statt als Schleife nur einmal abfragen?
Indem du die FOR /L Schleife um die 3 Zeilen einfach weglässt?
Und wenn du statt
echo %%i
ein
set "variablename=%%i"
schreibst, hast du den Name des Prozesses in einer Variablen. Und wenn du die Ausgabe der Prozess ID nicht brauchst, schmeißt du sie eben raus. Was weiß ich ... Das hast du doch selbst durch Ausprobieren 10x schneller geklärt, als hier auf eine Antwort zu warten.

Steffen