:############################################################################## :# # :# Filename: GetTitle.bat # :# # :# Description: Get the current command console title # :# # :# Notes: When invoked as a batch file from a command prompt, # :# this reports the temporary title including GetTitle.bat. # :# # :# Uses the tasklist command, available with XP and later OS.# :# Temporarily modifies the title string. # :# # :# History: # :# 2010-12-22 JFL Original version, written by Jean-François Larvoire. # :# 2010-12-22 JFL Use tasklist's own filtering options. # :# # :############################################################################## @echo off setlocal set "ARG0=%~f0" set "FUNCTION=rem" set "RETURN=goto :eof" goto main :# Function GetProcess: Set PID and TITLE with the current console PID and title string. :GetProcess %FUNCTION% GetProcess setlocal enableextensions :# Get the list of command prompts titles for /f "tokens=2,9,*" %%a in ('tasklist /v /nh /fi "IMAGENAME eq cmd.exe"') do set TITLE.%%a=%%c :# Change the current title to a statistically unique value set TITLE=%RANDOM% %RANDOM% %RANDOM% %RANDOM% title %TITLE% :# Find our PID for /f "tokens=2" %%a in ('tasklist /v /nh /fi "WINDOWTITLE eq %TITLE%"') do set PID=%%a :# Find our initial title call set TITLE=%%TITLE.%PID%%% :# Restore the title title %TITLE% endlocal & set PID=%PID% & set TITLE=%TITLE% %RETURN% :main call :GetProcess echo PID=%PID% echo TITLE=%TITLE%