Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ::: Batch script to reposition console, includes macro to output text Aligned Right, Centre or Left.
- ::: Updated to facilitate multiple resize / repositions from within the same script with ease, Using a tempory Batch file a a 'middle man'
- ::: The tempory batch is started with the new console properties, and then Calls the original Batch with parameters for
- ::: Console width and :Label to resume from
- ::: Script fetches the current screen resolution and calculates the approximate X / Y coordinates needed to position
- ::: the console window in the centre of the screen.
- ::: Example output in my answer here: https://stackoverflow.com/questions/24228294/batch-centre-resize-and-reposition/61046030#61046030
- ::: Script updated to allow positioning of console at screen top left with any 3rd parameter
- ::: Note : If called or started from cmd.exe or another batch, this script will end the parent Process.
- @Echo Off & CD "%~dp0"
- Set "AlignFile=%~dpnx0"
- Setlocal DisableDelayedExpansion
- (Set LF=^
- %= NewLine =%)
- Set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
- %= Define console width and values for text alignment =%
- Set @Align_Centre=Set /A "%%H=(Console_Width / 2) - (Len / 2)"
- Set @Align_Right=Set /A "%%H=(Console_Width - Len)"
- Set @Align_Left=Set /A "%%H=0"
- %= @Align Macro calculates string length then uses 2nd Parameter to Act on Alignment Calculation =%
- %= Macro appends spaces to the string depending on Alignment value / mode chosen to position, then output string to console. =%
- Set @Align=for /L %%n in (1 1 2) do if %%n==2 (%\n%
- For /F "tokens=1,* delims=, " %%G in ("!argv!") do (%\n%
- If not "!%%~G!"=="" (Set "TextOut=!%%~G!") Else (Set "TextOut=%%~G")%\n%
- Set LenTrim=Start%\n%
- For /L %%a in (1,1,!Console_Width!) Do (%\n%
- IF NOT "!LenTrim!"=="" (%\n%
- Set LenTrim=!TextOut:~0,-%%a!%\n%
- If "!LenTrim!"=="" Set "Len=%%a"%\n%
- ) %\n%
- ) %\n%
- IF /I "%%H"=="C" %@Align_Centre% %\n%
- IF /I "%%H"=="R" %@Align_Right% %\n%
- IF /I "%%H"=="L" %@Align_Left% %\n%
- For /L %%# in (1,1,!%%H!) Do Set "TextOut= !TextOut!" %\n%
- Echo(!TextOut!^&^& Endlocal %\n%
- ) %\n%
- ) ELSE setlocal enableDelayedExpansion ^& set argv=,
- If Not "%~2"=="" (Set "Console_Width=%~2" & Goto :%~1) Else (Goto :main)
- %= Subroutine to process output of wmic command into usable variables for screen dimensions (resolution) =%
- :ChangeConsole <Lines> <Columns> <Label to Resume From> <If a 4th parameter is Defined, Aligns screen at top left>
- %= Get screen Dimensions =%
- For /f "delims=" %%# in ('"wmic path Win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution /format:value"') do (
- Set "%%#">nul
- )
- %= Calculation of X axis relative to screen resolution and console size =%
- Set /A CentreX= ( CurrentHorizontalResolution / 2 ) - ( %~2 * 4 )
- %= Sub Optimal calculation of Y axis relative to screen resolution and console size =%
- For /L %%A in (10,10,%1) DO Set /A VertMod+=1
- Set /A CentreY= ( CurrentVerticalResolution / 4 ) - ( %~1 * Vertmod )
- %= Optional 4th parameter can be used to align console at top left of screen instead of screen centre =%
- If Not "%~4"=="" (Set /A CentreY=0,CentreX=-8)
- %= .Vbs script creation and launch to reopen batch with new console settings, combines with =%
- Set "Console_Width=%~2"
- %= Creates a batch file to reopen the main script using Call with parameters to define properties for console change and the label to resume from =%
- (
- Echo.@Mode Con: lines=%~1 cols=%~2
- Echo.@Title Res: %CurrentHorizontalResolution%x%CurrentVerticalResolution% X,Y Pos: %CentreX%,%CentreY% Con Size: Cols = %~2 Lines = %~1
- Echo.@Call "%AlignFile%" "%~3" "%~2"
- )>"%temp%\ChangeConsole.bat"
- (
- Echo.Set objWMIService = GetObject^("winmgmts:\\.\root\cimv2"^)
- Echo.Set objConfig = objWMIService.Get^("Win32_ProcessStartup"^)
- Echo.objConfig.SpawnInstance_
- Echo.objConfig.X = %CentreX%
- Echo.objConfig.Y = %CentreY%
- Echo.Set objNewProcess = objWMIService.Get^("Win32_Process"^)
- Echo.intReturn = objNewProcess.Create^("%temp%\ChangeConsole.bat", Null, objConfig, intProcessID^)
- )>"%temp%\Consolepos.vbs"
- %= Starts the companion batch script to Change Console properties, ends the parent =%
- Start "" "%temp%\Consolepos.vbs" & Exit
- :main
- %= If a 4rd parameter is used, Console will be positioned at top left of screen =%
- Call :ChangeConsole 45 50 Display_Text_1 top
- :Display_Text_1
- For %%B in ("Show this" "in centre") do Set "Text=%%~B" & %@Align% Text C
- For %%B in ("Show this" "on right") do Set "Text=%%~B" & %@Align% Text R
- For %%B in ("Show this" "on left") do Set "Text=%%~B" & %@Align% Text L
- Echo.Example 1 Done. & Pause >nul
- Call :ChangeConsole 25 150 Display_Text_2
- :Display_Text_2
- Set "string=<< %%A Left String%% \" & %@Align% string L
- Set "string=|^ A Centred String ^|" & %@Align% string C
- Set "string=/ A !Right String!>>" & %@Align% string R
- Echo.Example 2 Done. & Pause >nul
- (taskkill /pid WScript.exe /f /t) >nul 2>nul
- Timeout 1 >nul
- Del /F "%temp%\Consolepos.vbs" >nul 2>nul
- Del /F "%temp%\ChangeConsole.bat" >nul 2>nul
- exit /b
Advertisement
Add Comment
Please, Sign In to add comment