Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for paddemo.bat

(view source code of paddemo.bat as plain text)

  1. @ECHO OFF
  2. CLS
  3. ECHO Demo batch file for the subroutines LeftPad and RightPad. Both subroutines
  4. ECHO require 3 arguments: [1] variable name, [2] required length, [3] padding
  5. ECHO character. After running one of the subroutines, the length of the variable's
  6. ECHO value will equal the specified length, and if necessary the value is padded
  7. ECHO with the required amount of padding characters.
  8. ECHO Note: the following characters cannot be used as padding characters: ^(^<^&^^^"^|^>^)
  9. ECHO.
  10. ECHO Written by Rob van der Woude
  11. ECHO http://www.robvanderwoude.com
  12. ECHO.
  13.  
  14. SET Test=0123456789
  15. SET Test
  16. ECHO LeftPad Test 13 "+"
  17. CALL :LeftPad Test 13 "+"
  18. SET Test
  19. SET Test=0123456789
  20. ECHO LeftPad Test 4 "+"
  21. CALL :LeftPad Test 4 "+"
  22. SET Test
  23. ECHO.
  24.  
  25. SET Test=ABCD
  26. SET Test
  27. ECHO RightPad Test 7 "-"
  28. CALL :RightPad Test 7 "-"
  29. SET Test
  30. SET Test=ABCD
  31. ECHO RightPad Test 3 "-"
  32. CALL :RightPad Test 3 "-"
  33. SET Test
  34. ECHO.
  35.  
  36. :: End of main program
  37. GOTO:EOF
  38.  
  39.  
  40. :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = ::
  41. :: = :: = :: = :: = :: = :: =    Subroutines    = :: = :: = :: = :: = :: = ::
  42. :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = :: = ::
  43.  
  44.  
  45. :LeftPad
  46. :: SET Var=1234
  47. :: CALL :LeftPad Var 7 "x"
  48. :: Result: Var will equal 1234xxx
  49. :: SET Var=1234
  50. :: CALL :LeftPad Var 3 "x"
  51. :: Result: Var will equal 123
  52. ::
  53. :: Written by Rob van der Woude
  54. :: http://www.robvanderwoude.com
  55. ::
  56. SETLOCAL ENABLEDELAYEDEXPANSION
  57. CALL SET LeftPad=%%%1%%
  58. FOR /L %%? IN (0,1,%2) DO SET LeftPad=!LeftPad!%~3
  59. SET LeftPad=!LeftPad:~0,%2!
  60. ENDLOCAL & SET %1=%LeftPad%
  61. GOTO:EOF
  62.  
  63.  
  64. :RightPad
  65. :: SET Var=1234
  66. :: CALL :RightPad Var 7 "x"
  67. :: Result: Var will equal xxx1234
  68. :: SET Var=1234
  69. :: CALL :RightPad Var 3 "x"
  70. :: Result: Var will equal 234
  71. ::
  72. :: Written by Rob van der Woude
  73. :: http://www.robvanderwoude.com
  74. ::
  75. SETLOCAL ENABLEDELAYEDEXPANSION
  76. CALL SET RightPad=%%%1%%
  77. FOR /L %%? IN (0,1,%2) DO SET RightPad=%~3!RightPad!
  78. SET RightPad=!RightPad:~-%2!
  79. ENDLOCAL & SET %1=%RightPad%
  80. GOTO:EOF
  81.  

page last modified: 2024-02-26; loaded in 0.0185 seconds