:: GET WINDOW TITLE 2 :: The batch displays the window title @ECHO OFF :: Define temp file and temp title name SET TmpFile=%~DP0TMP%RANDOM%.TXT :: Define temporary window title name. It must be long and random SET TmpName=%RANDOM%%RANDOM%%RANDOM%%RANDOM% :: save current processes with original window titles to the temp file in CSV format TASKLIST /FO "CSV" /V > "%TmpFile%" :: Change title of the window TITLE %TMPNAME% :: Get PID of current window with the new title FOR /F "tokens=2" %%A IN ('TASKLIST /V /FO "TABLE" ^|FIND "%TMPNAME%"') DO SET MyPID=%%A :: Search for a line with PID in saved process list and save the line to a variable OneLine :: To find the line FINDSTR must be used to search exactly PID in the file. :: PID is relatively shoer number and can appear several times in the file as a substring :: FINDSTR can search eactly the correct appearence i.e --> ,"PID", <-- FOR /F "tokens=*" %%A IN ('FINDSTR /R ",\"%MyPID%\"," "%TmpFile%"') DO SET OneLine=%%A :: Delete temporary file, it is no longer needed DEL "%TmpFile%" :: Variable OneLine contains a line in CSV format i.e. fields are in :: double quotes and separated by commas. However some fields can contain commas :: for example as a thousands separator. FOR cycle ignores double quotes, so :: number of separated fields vaired in the line and it is impossible to set :: correct token number to read the "windows title field" :: Solution: all double quotes are replaced by back quotes and then taken as the separator. FOR /F "tokens=17 delims=`" %%A IN ('ECHO %OneLine:"=`%') DO SET WindowTitle=%%A :: Remove from the WindowTile the batch name at the end CALL SET WindowTitleOrig=%%WindowTitle: - %~n0=%% :: Set back the original title TITLE %WindowTitleOrig% :: Echo the title ECHO %WindowTitle%