(view source code of paddemo.bat as plain text)
@ECHO OFF
CLS
ECHO Demo batch file for the subroutines LeftPad and RightPad. Both subroutines
ECHO require 3 arguments: [1] variable name, [2] required length, [3] padding
ECHO character. After running one of the subroutines, the length of the variable's
ECHO value will equal the specified length, and if necessary the value is padded
ECHO with the required amount of padding characters.
ECHO Note: the following characters cannot be used as padding characters: ^(^<^&^^^"^|^>^)
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO.
SET Test=0123456789
SET Test
ECHO LeftPad Test 13 "+"
CALL :LeftPad Test 13 "+"
SET Test
SET Test=0123456789
ECHO LeftPad Test 4 "+"
CALL :LeftPad Test 4 "+"
SET Test
ECHO.
SET Test=ABCD
SET Test
ECHO RightPad Test 7 "-"
CALL :RightPad Test 7 "-"
SET Test
SET Test=ABCD
ECHO RightPad Test 3 "-"
CALL :RightPad Test 3 "-"
SET Test
ECHO.
:: End of main program
GOTO:EOF
:: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = ::
:: = :: = :: = :: = :: = :: = Subroutines = :: = :: = :: = :: = :: = ::
:: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = ::
:LeftPad
:: SET Var=1234
:: CALL :LeftPad Var 7 "x"
:: Result: Var will equal 1234xxx
:: SET Var=1234
:: CALL :LeftPad Var 3 "x"
:: Result: Var will equal 123
::
:: Written by Rob van der Woude
:: http://www.robvanderwoude.com
::
SETLOCAL ENABLEDELAYEDEXPANSION
CALL SET LeftPad=%%%1%%
FOR /L %%? IN (0,1,%2) DO SET LeftPad=!LeftPad!%~3
SET LeftPad=!LeftPad:~0,%2!
ENDLOCAL & SET %1=%LeftPad%
GOTO:EOF
:RightPad
:: SET Var=1234
:: CALL :RightPad Var 7 "x"
:: Result: Var will equal xxx1234
:: SET Var=1234
:: CALL :RightPad Var 3 "x"
:: Result: Var will equal 234
::
:: Written by Rob van der Woude
:: http://www.robvanderwoude.com
::
SETLOCAL ENABLEDELAYEDEXPANSION
CALL SET RightPad=%%%1%%
FOR /L %%? IN (0,1,%2) DO SET RightPad=%~3!RightPad!
SET RightPad=!RightPad:~-%2!
ENDLOCAL & SET %1=%RightPad%
GOTO:EOF
page last modified: 2024-04-16; loaded in 0.0053 seconds