String processing in windows batch files: How to pad value with leading zeros?

前端 未结 7 580
时光取名叫无心
时光取名叫无心 2020-11-27 19:07

in a Windows cmd batch file (.bat), how do i pad a numeric value, so that a given value in the range 0..99 gets transformed to a string in the range \"00\" to \"99\". I.e. I

相关标签:
7条回答
  • 2020-11-27 19:29

    Previous answers had explained all the existent methods to pad a value with left zeros; I just want to add a small trick I used to do that in an easier way. What had not been enough mentioned in previous answers is that in most cases, the value that will be padded is incremented inside a loop and that the padded value is just used to display it (or similar tasks, like renames). For example, to show values from 00 to 99:

    set x=0
    :loop
       rem Pad x value, store it in padded
       set padded=0%x%
       set padded=%padded:~-2%
       rem Show padded value
       echo %padded%
       set /A x+=1
    if %x% leq 99 goto loop
    

    If this is the case, the value of the variable may be used for both control the loop and display its padded value with no modification if its limits are appropriately translated. For example, to show values from 00 to 99:

    set x=100
    :loop
       rem Show padded value
       echo %x:~-2%
       set /A x+=1
    if %x% leq 199 goto loop
    

    This method works also with any number of left zeros to pad.

    Antonio

    0 讨论(0)
  • 2020-11-27 19:34

    There's a two-stage process you can use:

    REM initial setup
    SET X=5
    
    REM pad with your desired width - 1 leading zeroes
    SET PADDED=0%X%
    
    REM slice off any zeroes you don't need -- BEWARE, this can truncate the value
    REM the 2 at the end is the number of desired digits
    SET PADDED=%PADDED:~-2%
    

    Now TEMP holds the padded value. If there's any chance that the initial value of X might have more than 2 digits, you need to check that you didn't accidentally truncate it:

    REM did we truncate the value by mistake? if so, undo the damage
    SET /A VERIFY=1%X% - 1%PADDED%
    IF NOT "%VERIFY%"=="0" SET PADDED=%X%
    
    REM finally update the value of X
    SET X=%PADDED%
    

    Important note:

    This solution creates or overwrites the variables PADDED and VERIFY. Any script that sets the values of variables which are not meant to be persisted after it terminates should be put inside SETLOCAL and ENDLOCAL statements to prevent these changes from being visible from the outside world.

    0 讨论(0)
  • 2020-11-27 19:36
    @echo off
    rem .
    rem   counter example - with and without padding (up to 260 leading 0s which should be enough for most filenames)
    rem .
    rem   we assume values given are valid
    rem   additional error checking could be done to make sure they are numbers
    rem   and to ensure that starting is less than ending
    rem   and that the number of ending digits is not greater than the number of padding digits
    rem .
    if "%2"=="" (
      echo.
      echo usage:   %~nx0 [starting number] [ending number] [pad]
      echo example: %~nx0  0  19        will output numbers 0 to 19 each on a new line
      echo example: %~nx0  3  12  8     will output numbers 3 to 12 each on a new line padded to 8 digits
      echo.
      goto end
      )
    rem .
    setlocal enabledelayedexpansion
    if "%3"=="" (
      for /l %%x in (%1, 1, %2) do (
        echo.%%x
      )
    ) else (
      set "mynum="
      for /l %%x in (%1, 1, %2) do (
        call set "mynum=00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000%%x"
        call set "mynum=%%mynum:~-%3%%"
        call echo.%%mynum%%
      )
    )
    :end
    
    0 讨论(0)
  • 2020-11-27 19:37

    The single line

    IF 1%Foo% LSS 100 SET Foo=0%Foo%
    

    will get you what you want for numbers in the range that you specify. It does not change values in the subset 0-9 if they are already single-padded.

    0 讨论(0)
  • 2020-11-27 19:40

    This example uses a for loop to demonstrate, but the logic is the same even if you were to use it without the loop. Just echo a 0 in front if the number is less than 10.

    setlocal enabledelayedexpansion
    for /l %%a in (1,1,40) do (
    set n=%%a
    if !n! lss 10 (
    echo 0!n!
    ) else (
    echo !n!
    )
    )
    pause >nul
    
    0 讨论(0)
  • 2020-11-27 19:43

    OK GUYS i have found a solution, compressing it down as simple as possible.

    @echo off
    title pad numbers
    set num=0
    set zero= 000
    :loop
    @set /a num=%num%+1
    if /i %num% GTR 9 set zero= 00
    if /i %num% GTR 99 set zero= 0
    if /i %num% GTR 999 set zero= 
    echo %zero%%num%
    goto loop
    

    this will display your count up number using 4 digits. but the code can be altered to use 2 digits as shown below.

    @echo off
    title pad numbers
    set num=0
    set zero= 0
    :loop
    @set /a num=%num%+1
    if /i %num% GTR 9 set zero= 
    echo %zero%%num%
    goto loop
    

    if you want to set it as a displayable single variable...

    @echo off
    title pad numbers
    set num=0
    set zero= 0
    :loop
    @set /a num=%num%+1
    if /i %num% GTR 9 set zero= 
    set %zero%%num%=number
    echo %number%
    goto loop
    

    if you want it to count up in seconds...

    @echo off
    title pad numbers
    set num=0
    set zero= 0
    :loop
    @set /a num=%num%+1
    if /i %num% GTR 9 set zero= 
    set %zero%%num%=number
    echo %number%
    ping localhost -n 2 >nul
    goto loop
    

    i hope this was a great help ^^

    0 讨论(0)
提交回复
热议问题