display title and pid of current window?
Posted: 11 Dec 2012 12:00
how can I display the title and pid of the current window?
A Forum all about DOS Batch
https://www.dostips.com/forum/
Code: Select all
@echo off &setlocal
title This is a test.
setlocal EnableDelayedExpansion
set "uid="
for /l %%i in (1 1 128) do (set /a "bit=!random!&1" &set "uid=!uid!!bit!")
for /f "tokens=2 delims==" %%i in (
'WMIC Process WHERE "Name='cmd.exe' AND CommandLine LIKE '%%!uid!%%'" GET ParentProcessID /value'
) do for /f %%j in ("%%i") do (endlocal &set "PID=%%j")
for /f "tokens=1* delims=:" %%i in ('tasklist /fo "LIST" /fi "PID eq %PID%" /v') do (
for /f "tokens=*" %%k in ("%%j") do set "WindowTitle=%%k"
)
echo PID = %PID%
echo WindowTitle = "%WindowTitle%"
pause
Code: Select all
for /f "skip=1tokens=3" %%? in ('"!$cmdow.file!" @') do set "_=%%?"
set "_"
Code: Select all
@echo off
for /F "delims=" %%a in ('Window') do set title=%%a
rem This file is PID.BAT
set title=%title: - PID=%
set randNum=%random%
title %randNum%
for /F "tokens=2" %%a in ('tasklist /FI "WINDOWTITLE eq %randNum%"') do set PID=%%a
title %title%
echo Title: "%title%"
echo PID: %PID%
Code: Select all
@echo off & setlocal
set r=%random%_%time:,=%_%random%
for /f "tokens=2 delims=," %%i in ('title %r%^&tasklist /v /fo csv /nh ^|find "%r%"') do echo PID=%%~i
One sees a window TITLE when at the DOS Prompt. That is what I wish to retrieve and although it visibly changes while I am running a command that does not specifically change the TITLE. It is also what I expect it to be after the command returns to Prompt. The obstacle to getting this simple string is the format in which TASKLIST presents the "Window Title" value. All of the contributed methods successfully retrieve this value in the same format. Even if disguised by cmdow.exe or window.exe they all seem to rely on the TASK manager for this data.Ed Dyreen wrote:You can use a native solution using tasklist or non native using cmdow.EXE.
I do not know of a bulletproof tasklist solution.ed
Code: Select all
Output from cmdow.exe using findEDtitle.bat:
Handle Lev Pid -Window status- Image Caption
0x4D06DE 1 1084 Res Act Ena Vis cmd Command Prompt - findEDtitle
Title from TASKLIST findtitle.bat: 'Command Prompt - findtitle'
Actual Title of window: "Command Prompt"
Code: Select all
:: formatted for full title
%$cmdow.file% @ /b
for /f "tokens=8*" %%a in ('cmdow @ /b') do set "_=%%b"
set "_"
call TrimTitle EDtitle "%_%"
Echo/Title: '%EDtitle%'
Code: Select all
:TrimTitle TITLEvariableName[out] RawTITLEValue[In]
@echo Off & SetLocal EnableDelayedExpansion
:: Post subject: Re: display title and pid of current window?
:: http://www.dostips.com/forum/viewtopic.php?p=22671#p22671
:: Posted: Mon Aug 30, 2016 by thefeduke
Set "WindowTitle=%~2"
Set "WindowTitle=%WindowTitle:"=""%"
Rem.Removing up to and including " - " from the TASKLIST Window Title.
Set "CommandName= %WindowTitle:* - =%"
For /L %%a In (1,1,9) Do (
Set "ChopTitle=!CommandName:* - =!"
Rem.Cycling to find last " - " to isolate the command portion.
If " !ChopTitle!" NEQ "!CommandName!" Set "CommandName=!ChopTitle!"
)
Set "WindowTitle=!WindowTitle: - %ChopTitle%=!
Rem.One of these trims should work depending on the Space/Dash continuum.
Set "WindowTitle=!WindowTitle: -%ChopTitle%=!
Set "WindowTitle=%WindowTitle:""="%"
(EndLocal & Rem.Save "%~1" value.
If "%~1" NEQ "" (SET "%~1=%WindowTitle%") ELSE Set "Title=%WindowTitle%")
Exit /B
Code: Select all
:FindTitle TITLEvariableName[out][Opt] PIDvariableName[Out][Opt]
@echo Off & setlocal EnableDelayedExpansion
:: Post subject: Re: display title and pid of current window?
::http://www.dostips.com/forum/viewtopic.php?p=22671#p22671
::Posted: Mon Aug 30, 2016 by thefeduke
Call :WindowLists
Call :WindowPid mypid
Call :WindowTitle MyWindowTitle "%MyPid%"
Echo/
Echo/Current Window PID and TITLE:
Echo/ PID : %myPid%
Echo/Title from TASKLIST: '%MyWindowTitle%'
Echo/
rem.For the preplanned TASKLIST output adjustment to work,
rem.if this file is FindTitle.bat it has to be called with the command: findtitle
rem.It is not case-sensitive and can also be called with: call Findtitle
set title=%MyWindowTitle: - findtitle=%
rem.TASKLIST adds an extra space between call and the command.
set title=%title: - call findtitle=%
::Posted: Tue Dec 11, 2012 10:38 pm by Aacini
Echo.Adjusted Title^(using a preplanned naming method^): '%Title%'
>>"%Temp%\%~n0_RawWindows.txt" Echo.Done preplanned method
Call :TrimTitle MyWindowTitle "%MyWindowTitle%"
Echo.Adjusted Title ^(using a more generalized method^): '%MyWindowTitle%'
Echo/
(EndLocal & Rem.Save values if requested.
If "%~1" NEQ "" SET "%~1=%MyWindowTitle%"
If "%~2" NEQ "" SET "%~2=%MyPid%"
)
pause
Goto :EOF
:WindowLists [No Arguments] produces temporary output files
rem.Save a PID/TITLE list because a following temporary TITLE change
rem.prevents real TITLE access for duration of the script.
@echo Off & SetLocal EnableDelayedExpansion
>"%Temp%\%~n0_Windows.txt" Echo.
>"%Temp%\%~n0_RawWindows.txt" Echo.
For /F "delims=" %%i IN (
'TASKLIST /FI "imagename eq cmd.exe" /v /fo list ^| FindStr "PID: Title"'
) do (
Set "WinPID=%%i"
If /I "!WinPID:~0,4!" EQU "PID:" Set "lastPid=!WinPID:~14!"
If /I "!WinPID:~0,13!" EQU "Window Title:" (
Set "Pid=!lastPid: =!"
IF /I "!WinPID:~14,3!" NEQ "N/A" (
>>"%Temp%\%~n0_Windows.txt" Echo.!Pid! !WinPID:~14!
)
>>"%Temp%\%~n0_RawWindows.txt" Echo.Pid: !Pid! %%i
)
)
>>"%Temp%\%~n0_RawWindows.txt" Echo.Exiting :WindowLists
Exit /B
:WindowPid PIDVariableName[In,Out]
@echo Off & SetLocal
:: Credit to Post: Tue Dec 11, 2012 4:06 pm by aGerman
rem.for /l %%i in (1 1 128) do (set /a "bit=!random!&1" &set "QuiteUnique=!QuiteUnique!!bit!")
set "QuiteUnique=Quite%random%Unique"
:: Credit to Post: Fri Mar 14, 2014 3:50 pm by einstein1969
:: Credit to Post: Tue Dec 11, 2012 10:38 pm by Aacini
for /f "tokens=2 delims=," %%i in (
'title %QuiteUnique%^&tasklist /v /fo csv /nh ^|find "%QuiteUnique%"'
) do Set PID=%%~i
>>"%Temp%\%~n0_RawWindows.txt" Echo.Exiting :WindowPid Setting PId=%PID%
(EndLocal & Rem.Save "%~1" value.
If "%~1" NEQ "" (SET "%~1=%Pid%") ELSE Echo/PID not determined.)
>>"%Temp%\%~n0_RawWindows.txt" Echo.Exiting :WindowPID after EndLocal
Exit /B
:WindowTitle TITLEVariableName[In,Out] PIDValue[In]
@echo Off & SetLocal
>>"%Temp%\%~n0_RawWindows.txt" Echo.Entering :WindowTitle Seeing PId=%~2 to Set %~1
For /F "usebackq tokens=1* delims= " %%i IN (`Type "%Temp%\%~n0_Windows.txt"`
) do If /I "%%i" EQU "%~2" Set "WindowTitle=%%j"
>>"%Temp%\%~n0_RawWindows.txt" Echo.Exiting :WindowTitle Setting %~1 to %WindowTitle%
(EndLocal & Rem."%~2" Was only an input value
If "%~1" NEQ "" (SET "%~1=%WindowTitle%") ELSE Set "WinTitle=%WindowTitle%")
>>"%Temp%\%~n0_RawWindows.txt" Echo.Exiting :WindowTitle after EndLocal
Exit /B
:TrimTitle TITLEvariableName[out] RawTITLEValue[In]
@echo Off & SetLocal EnableDelayedExpansion
:: Post subject: Re: display title and pid of current window?
:: http://www.dostips.com/forum/viewtopic.php?p=22671#p22671
:: Posted: Mon Aug 29, 2016 by thefeduke
Set "WindowTitle=%~2"
Set "WindowTitle=%WindowTitle:"=""%"
Rem.Removing up to and including " - " from the TASKLIST Window Title.
Set "CommandName= %WindowTitle:* - =%"
For /L %%a In (1,1,4) Do (
Set "ChopTitle=!CommandName:* - =!"
Rem.Cycling to find last " - " to isolate the command portion.
If " !ChopTitle!" NEQ "!CommandName!" Set "CommandName=!ChopTitle!"
)
Set "WindowTitle=!WindowTitle: - %ChopTitle%=!
Rem.One of these trims should work depending on the Space/Dash continuum.
Set "WindowTitle=!WindowTitle: -%ChopTitle%=!
Set "WindowTitle=%WindowTitle:""="%"
(EndLocal & Rem.Save "%~1" value.
If "%~1" NEQ "" (SET "%~1=%WindowTitle%") ELSE Set "Title=%WindowTitle%")
>>"%Temp%\%~n0_RawWindows.txt" Echo.Exiting :TrimTitle
Exit /B
thefeduke wrote:I know that this topic lay dormant for a few years
www.RobVanDerWoude.com wrote:
- must still function if multiple console windows are open
- if possible, must even function correctly if multiple instances of the batch file are running
- must take into account (and strip) the Administrator: prefix added in UAC enabled Windows versions
. . .
All "contestants" seem to agree on the use of TASKLIST to retrieve the window title.
Correctly running multiple instances of the batch files was the hardest requirement to meet.