0

Exactly what the title says. I have a batch script that changes the console's window resolution, title, and text colour. When it closes, I'd like my script to revert all of that back to what the user originally had theirs at. How possible is this?

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
ditheredtransparency
  • 475
  • 3
  • 10
  • 18
  • Please go back to all of your previous questions and accept answers for them. Extremely impolite not to do so. – Squashman Oct 14 '17 at 15:30
  • I understand, but I'm not going to accept an answer until I can verify that the answer(s) provided fulfill my question. I've done that before, and then I realise that something isn't working, so I ask again and I don't get a response. – ditheredtransparency Oct 14 '17 at 21:38
  • As long as you're talking about what the user would see when they opened the command prompt for the first time, that is possible. If you're talking about taking the _current_ values at the start of the script (after the user changed them between opening the command prompt and starting the script), that is not possible in batch. – SomethingDark Oct 17 '17 at 21:15
  • Ah, ok. Thank you SomethingDark. So how could I change it back to what they would see when first starting CMD? – ditheredtransparency Oct 17 '17 at 21:22

2 Answers2

1

You could save all of that data in various text files, like so.

set /p title=Create a Title: 
title %title%
break >"title.txt"
echo %title% >>"title.txt"

This will ask the user for their preferred title name, and this title name will be saved in a text file. Now to make it set that as the title from now on you could do this.

if exist "title.txt" (
    set /p title1=<title.txt
    title %title1%
)
set /p title=Create a Title:
title %title% 
break >"title.txt"
echo %title% >>"title.txt"

This will first test if the user has yet specified a title in the past, if so then it will change the title accordingly, if not it will prompt the user for a title.

Randy Matinee
  • 71
  • 1
  • 5
  • I appreciate the answer, but I want my script to automatically store the current title as a variable so it can be restored when the script is finished. – ditheredtransparency Oct 14 '17 at 21:39
0

These are stored across a couple of places, but are mostly located in the registry under HKCU\Console. Also, by default the cmd title is just the path to cmd.exe, but if you opened it via a shortcut, the title changes to whatever the shortcut is named. Unfortunately, the location of the shortcut changes with every Windows version and it isn't possible to determine how the command prompt was opened, so we're sticking with the default default title.

Either stick your other code in the section marked "YOUR OTHER CODE HERE" or just have a line that says call yourscript.bat.

@echo off

::------------------------------------------------------------------------------
:: Store default values from the registry
::------------------------------------------------------------------------------
call :get_rows_and_columns WindowSize
call :get_rows_and_columns ScreenBufferSize
call :get_HKCU_Console_value ScreenColors
set "default_colors=%hkcu_value:~-2%"

:: Leading zeroes get removed in a reg query which makes things complicated
:: when the default background color is black
set "default_colors=%default_colors:x=0%"

::------------------------------------------------------------------------------
:: YOUR OTHER CODE HERE
::------------------------------------------------------------------------------


::------------------------------------------------------------------------------
:: RESTORE CMD TO ITS ORIGINAL APPEARANCE
::------------------------------------------------------------------------------
call :resize_console %WindowSize_cols% %WindowSize_rows% %ScreenBufferSize_cols% %ScreenBufferSize_rows%

:: Google says the default cmd window title is the path to cmd.exe, which is
:: stored in %COMSPEC%, but I've also seen it be based on the name of the
:: shortcut in %APPDATA%\Microsoft\Windows\Start Menu\Programs\System Tools
:: in Windows 10 or %APPDATA%\Microsoft\Windows\Start Menu\Programs\Accessories
:: in Windows Vista. I don't have a 7, 8, or 8.1 VM so I don't know the paths
:: for those.
title %COMSPEC%
pause
exit /b

::------------------------------------------------------------------------------
:: Gets the registry value of a specified key
::
:: Arguments: %1 - the key to search for
:: Returns:   The value of the registry key
::------------------------------------------------------------------------------
:get_HKCU_Console_value
set "hkcu_value="
for /f "tokens=3" %%A in ('reg query HKCU\Console /v %~1 ^| find "%~1"') do set "hkcu_value=%%A"
exit /b

::------------------------------------------------------------------------------
:: Calculates rows and columns of a screen size based on registry value.
:: According to https://stackoverflow.com/a/10664060/4158862, the decimal value
:: of the registry key is equal to (rows*65536)+columns.
::
:: Arguments: %1 - The registry key to search for
:: Returns:   The number of rows and columns used by that screen
::------------------------------------------------------------------------------
:get_rows_and_columns
set "key=%~1"
call :get_HKCU_Console_value "%key%"
set "%key%Size_hex=%hkcu_value%"
set /a %key%Size_dec=%key%Size_hex + 0
set /a %key%_cols=%key%Size_dec %% 65536
set /a %key%_rows=%key%Size_dec / 65536
exit /b

::------------------------------------------------------------------------------
:: Adjusts the size of both the command prompt window and its line buffer
:: From https://stackoverflow.com/a/13351373/4158862 
::
:: Arguments: %1 - Columns in cmd screen width
::            %2 - Rows in cmd screen width
::            %3 - Columns in buffer width
::            %4 - Rows in cmd screen width
:: Returns:   None
::------------------------------------------------------------------------------
:resize_console
mode con: cols=%1 lines=%2
powershell -command "&{$H=get-host;$W=$H.ui.rawui;$B=$W.buffersize;$B.width=%3;$B.height=%4;$W.buffersize=$B;}"
exit /b
SomethingDark
  • 13,229
  • 5
  • 50
  • 55