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

前端 未结 7 651
时光取名叫无心
时光取名叫无心 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: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
    

提交回复
热议问题