JSI Tip 5103. General purpose zero fill (padding) routine.

In tip 5100, I used padding to left zero fill two fields and insure that they had the same length.

I have scripted PadIt.bat to left zero fill any variable and set its length to a defined size.

The syntax for using PadIt.bat is:

call Padit Variable size

where Variable is any variable or constant and size is the final variable size. PadIt.bat returns the padded variable is the $Pad environment variable.

Rules of use

- The maximum size of the variable is arbitrarily set as 32.

- The size parameter may not start with a zero (0).

- The Variable parameter may NOT contain more characters that Size.

- If the parameters are in error, $Pad will NOT exist (if not defined %$Pad% goto label).

PadIt.bat contains:

@echo off
if \{%2\}==\{\} goto syntax
set $Pad=%1
set size=%2
if "%size%" EQU "0" goto syntax
if "%size:~0,1%" EQU "0" goto syntax
set /a size=%size%
If %size% GTR 32 goto syntax
call set wrk=%%$pad:~0^,%size%%%
if "%wrk%" NEQ "%$Pad%" goto error
set /a size=%size% + 1
set $Pad=%$Pad% 000000000000000000000000000000000000000000
for /f "tokens=1,2" %%s in ('@echo %%$Pad:~^,%size%%%') do set $Pad=%%t%%s
endlocal&set $Pad=%$Pad%
goto :EOF
@echo PadIt ^%%VAR^%% Size
endlocal&set $Pad=
goto :EOF
@echo PadIt ^%%VAR^%% Size - variable has more than 'size' characters
endlocal&set $Pad=

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.