29

在 Windows cmd 批处理文件 (.bat) 中,我如何填充数值,以便将 0..99 范围内的给定值转换为“00”到“99”范围内的字符串。即我想为低于 10 的值设置前导零。

4

8 回答 8

28

您可以使用两个阶段的过程:

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%

现在TEMP保存填充值。如果 的初始值有X可能超过 2 位,您需要检查您是否不小心截断了它:

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%

重要的提示:

此解决方案创建或覆盖变量PADDEDVERIFY. 任何设置变量值的脚本在它终止后不打算保留,都应该放在里面SETLOCAL,并ENDLOCAL声明以防止这些更改从外部世界可见。

于 2012-11-15T13:43:29.493 回答
17

如果您确信原始数字中的位数始终 <= 2,那么

set "x=0%x%"
set "x=%x:~-2%"

如果数字可能超过 2 位,并且您想填充到 2 位,但不截断大于 99 的值,则

setlocal enableDelayedExpansion
if "%x%" equ "%x:~-2%" (
  set "x=0%x%"
  set "x=!x:~-2!"
)

或者没有延迟扩展,使用中间变量

set paddedX=0%x%
if "%x%" equ "%x:~-2%" set "x=%paddedX:~-2%"

上述算法的好处是可以将填充扩展到任意宽度是微不足道的。例如,要填充到宽度 10,只需在前面加上 9 个零并保留最后 10 个字符

set "x=000000000%x%"
set "x=%x:~-10%"

防止截断

set paddedX=000000000%x%
if "%x%" equ "%x:~-10%" set "x=%paddedX:~-10%"
于 2012-11-15T15:49:41.180 回答
8

单行

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

将在您指定的范围内为您提供所需的数字。如果它们已经是单填充的,它不会更改子集 0-9 中的值。

于 2015-04-25T12:09:33.747 回答
5

以前的答案已经解释了所有现有的用左零填充值的方法;我只是想添加一个我以前用更简单的方式来做的小技巧。以前的答案中没有提到的是,在大多数情况下,将被填充的值在循环内递增,并且填充的值仅用于显示它(或类似的任务,如重命名)。例如,要显示从 00 到 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

如果是这种情况,则变量的值可用于控制循环显示其填充值,如果其限制被适当地转换,则无需修改。例如,要显示从 00 到 99 的值:

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

此方法也适用于要填充的任意数量的左零。

安东尼奥

于 2012-11-16T05:55:05.987 回答
3

好的,伙计们,我找到了一个解决方案,尽可能简单地压缩它。

@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

这将使用 4 位数字显示您的计数。但代码可以更改为使用 2 位数字,如下所示。

@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

如果要将其设置为可显示的单个变量...

@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

如果你想让它在几秒钟内计数......

@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

我希望这是一个很大的帮助^^

于 2016-11-06T05:56:11.120 回答
1

这个例子使用了一个for循环来演示,但是即使你在没有循环的情况下使用它,逻辑也是一样的。如果数字小于 10,就echo在前面。0

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
于 2012-11-15T13:46:51.137 回答
0
@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
于 2015-03-14T20:06:38.663 回答
0

这要容易得多:

set build_b=00%your_value%
set padded_value=%build_b:~-2%
于 2021-05-06T13:59:41.063 回答