@echo off if not "%~1"=="" ( echo use no parameters exit /b ) SetLocal EnableDelayedExpansion set strings=0 for /f "tokens=*" %%a in ('tasklist /nh /v /fo "CSV"') do ( set titlestr=%%a set titlestr=!titlestr:*","=! set /a strings+=1 for %%b in (!strings!) do ( for /f "tokens=1 delims=," %%c in ("!titlestr!") do ( set pid[%%b]=%%c set pid[%%b]=!pid[%%b]:~0,-1! ) for /l %%a in (1 1 7) do set titlestr=!titlestr:*","=! set titlestr[%%b]=!titlestr:~0,-1! ) ) :makesure set checkstr=Just a temp title %random% tasklist /v /fo "CSV"| >nul find /i "!checkstr!" && goto makesure title !checkstr! for /f "tokens=*" %%a in ('tasklist /nh /v /fi "WINDOWTITLE eq !checkstr!" /fo "CSV"') do ( set finalpid=%%a set finalpid=!finalpid:*","=! for /f "tokens=1 delims=," %%c in ("!finalpid!") do ( set finalpid=%%c set finalpid=!finalpid:~0,-1! ) ) for /l %%a in (1 1 !strings!) do ( if "!pid[%%a]!"=="!finalpid!" ( set titlestr=!titlestr[%%a]! ) ) set test="cmd /c ""%~f0" "" :strip if /i not "!cmdcmdline!"=="!test:~1,-1!" if not "!titlestr!"=="!titlestr: - =!" ( if not "!titlestr:~-3!"==" - " ( set titlestr=!titlestr:~0,-1! goto strip ) else ( set titlestr=!titlestr:~0,-3! ) ) echo.!titlestr! title !titlestr! pause