Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ::: v1.0 macro for use within a batch file REM: More efficient
- @Echo Off & Title Batch text alignment macro.
- %= Outputs strings assigned to variables to the console at the chosen alignment position =%
- %= From my Answer here: https://stackoverflow.com/a/61046030/12343998 =%
- Setlocal DisableDelayedExpansion
- (Set LF=^
- %= NewLine =%)
- Set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
- %= Define console width and values for alignment =%
- For /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| findstr Columns`) do set "Console_Width=%%W"
- Set /A Console_Width+=0
- IF %Console_Width% GTR 160 Set "Console_Width=160"
- %= SubMacros to Facilitate Arithmetic operations during Macro Execution =%
- 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,160) 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=,
- %= Script Break. below lines are to demonstrate Usage only =%
- 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
- Set "string=This Left String" & %@Align% string @L
- Set "string=This Centre String" & %@Align% string @C
- Set "string=This Right String" & %@Align% string @R
- Pause
- Exit /B
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement