<http://www.netikka.net/tsneti/info/tscmd127.htm>
Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Wed 19-May-2010 19:47:59

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



127} How can I pad leading zeros or like to a variable?

The solution already is embedded within a couple of items in this FAQ collection, but let's revisit the question and make it a separate item. The code below adds leading zeros to the replaceable parameter %1 making it exactly five characters long.
  @echo off & setlocal enableextensions
  set var_=00000%1
  set var_=%var_:~-5%
  echo %var_%
  endlocal & goto :EOF

The output could be e.g.
  C:\_D\TEST>cmdfaq 23
  00023
or
  C:\_D\TEST>cmdfaq 12345678
  45678

Written much more generally with a subroutine we can have
  @echo off & setlocal enableextensions
  if "%~1"=="" (
    echo Usage: %~0 MyTestString
    goto :EOF)
  call :LeadVar "%~1" 0 5 padded_
  echo.%padded_%
  endlocal & goto :EOF
  ::
  :: ===============================================================
  :: Subroutine: A generic padding of a string

  :LeadVar MyString PadWith MaxLength return_
  setlocal enableextensions enabledelayedexpansion
    set PadWith=%~2
    set MaxLength=%~3
    for /L %%a in (1,1,%MaxLength%) do set MyString=!MyString!!PadWith!
    set MyString=%MyString%%~1
    set return_=!MyString:~-%MaxLength%!
  endlocal & set "%~4=%return_%" & goto :EOF

An example of the output:
  C:\_D\TEST>cmdfaq "12 4"
  012 4

Another take. See the usage FuncRightJustify function which is part of the WHEREIS.CMD script included in this collection. (It uses spaces instead of zeros.) Useful for justifying columns.
  :FuncRightJustify arg1 arg2
  setlocal enableextensions
    set number_=            %1
    set number_=%number_:~-12%
  endlocal & set "%2=%number_%" & goto :EOF

Just for the sake of interest, let's write also a G(nu)AWK solution to the problem.
  @echo off & setlocal enableextensions
  set var_=00000%1
  echo %var_%|gawk '{printf "@set var_=%%05d\n",$0}'>"%TEMP%\tmp$$$.cmd"
  for %%c in (call del) do %%c "%TEMP%\tmp$$$.cmd"
  echo %var_%
  endlocal & goto :EOF

The gawk snippet could be formulated in other ways as well. For example the more complicated
  gawk '{i="00000"$1}{p=length(i)}{printf "@set var_=%%s\n",substr(i,p-4,p)}

The task can for further demonstration be also solved with a VBScipt-aided formulation:
  @echo off & setlocal enableextensions
  ::
  set var_=%~1
  ::
  :: Build a Visual Basic Script

  set skip=
  set temp_=%temp%
  if defined mytemp if exist "%mytemp%\" set temp_=%mytemp%
  set vbs_=%temp_%\tmp$$$.vbs
  >"%vbs_%" findstr "'%skip%VBS" "%~f0"
  ::
  :: Run the script with Microsoft Windows Script Host Version 5.6

  for /f "tokens=*" %%a in ('
    cscript //nologo "%vbs_%" "%var_%"') do (
      set paddedvar_=%%a)
  ::
  :: Display the result

  echo %paddedvar_%
  ::
  :: Clean up

  for %%f in ("%vbs_%") do if exist %%f del %%f
  endlocal & goto :EOF
  '
  'The Visual Basic Script

  Set arg = WScript.Arguments 'VBS
  Wscript.Echo PadFn(arg(0),5) 'VBS
  '
  Function PadFn (str, n) 'VBS
    PadFn = Right(String(n-1,"0")&str,n) 'VBS
  End Function 'VBS

The output could be e.g.
  C:\_D\TEST>cmdfaq 23
  00023
or
  C:\_D\TEST>cmdfaq 12345678
  45678

Alternatively, we might wish to have
  Function PadFn (str, n) 'VBS
    If Len(str) > n Then 'VBS
      PadFn = str 'VBS
    Else 'VBS
      PadFn = Right(String(n-1,"0")&str,n) 'VBS
    End If 'VBS
  End Function 'VBS

The output could be e.g.
  C:\_D\TEST>cmdfaq 23
  00023
or
  C:\_D\TEST>cmdfaq 12345678
  12345678

A brief way of writing the VBS-aided solution
  @echo off & setlocal enableextensions
  set var_=%~1
  echo %var_%
  >"%temp%\tmp$$$.vbs" echo WScript.Echo Right(String(5-1,"0")^&"%var_%",5)
  for /f "tokens=* delims=" %%a in (
    'cscript //nologo "%temp%\tmp$$$.vbs"') do set var_=%%a
  for %%f in ("%temp%\tmp$$$.vbs") do if exist %%f del %%f
  echo %var_%
  endlocal & goto :EOF

The output could be e.g.
  C:\_D\TEST>cmdfaq 23
  23
  00023
or
  C:\_D\TEST>cmdfaq 12345678
  12345678
  45678