138701
Goto Top

Aktives Fenster ermitteln

Hallo zusammen,

Wie kann ich mit Batch ermitteln, welches Programm gerade im Vordergrund läuft?

Content-Key: 437276

Url: https://administrator.de/contentid/437276

Printed on: April 18, 2024 at 04:04 o'clock

Member: rubberman
rubberman Apr 05, 2019 at 20:34:34 (UTC)
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
Mitglied: 139374
139374 Apr 05, 2019 updated at 20:59:38 (UTC)
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 .... 🚑🤕
Member: rubberman
Solution rubberman Apr 05, 2019 updated at 21:53:52 (UTC)
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
Mitglied: 138701
138701 Apr 06, 2019 updated at 16:40:50 (UTC)
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
Member: rubberman
Solution rubberman Apr 06, 2019 updated at 17:58:22 (UTC)
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