Windows 批处理文件中的字符串处理:如何用前导零填充值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13398545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String processing in windows batch files: How to pad value with leading zeros?
提问by Scrontch
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'd like to having leading zeros for values lower than 10.
在 Windows cmd 批处理文件 (.bat) 中,如何填充数值,以便将 0..99 范围内的给定值转换为“00”到“99”范围内的字符串。即我想对低于 10 的值使用前导零。
采纳答案by Jon
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:
现在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%
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.
此解决方案创建或覆盖变量PADDED
和VERIFY
。任何设置在终止后不打算保留的变量值的脚本都应该放在SETLOCAL
andENDLOCAL
语句中,以防止这些更改从外部世界可见。
回答by dbenham
If you are confident that the number of digits in your original number is always <= 2, then
如果您确信原始号码的位数总是 <= 2,则
set "x=0%x%"
set "x=%x:~-2%"
If the number may exceed 2 digits, and you want to pad to 2 digits, but not truncate values larger then 99, then
如果数字可能超过 2 位,并且您想填充到 2 位,但不截断大于 99 的值,则
setlocal enableDelayedExpansion
if "%x%" equ "%x:~-2%" (
set "x=0%x%"
set "x=!x:~-2!"
)
Or without delayed expansion, using an intermediate variable
或者没有延迟扩展,使用中间变量
set paddedX=0%x%
if "%x%" equ "%x:~-2%" set "x=%paddedX:~-2%"
The nice thing about the above algorithms is it is trivial to extend the padding to any arbitrary width. For example, to pad to width 10, simply prepend with 9 zeros and preserve the last 10 characters
上述算法的好处是将填充扩展到任意宽度是微不足道的。例如,要填充宽度为 10,只需在前面加上 9 个零并保留最后 10 个字符
set "x=000000000%x%"
set "x=%x:~-10%"
TO prevent truncating
防止截断
set paddedX=000000000%x%
if "%x%" equ "%x:~-10%" set "x=%paddedX:~-10%"
回答by user3168191
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-9 中的值已经单填充,则不会更改它们。
回答by Aacini
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 loopand that the padded value is just used to display it(or similar tasks, like renames). For example, to show values from 00 to 99:
以前的答案已经解释了用左零填充值的所有现有方法;我只是想添加一个小技巧,我曾经以更简单的方式做到这一点。之前的答案中没有提到的是,在大多数情况下,将填充的值在循环内递增,并且填充的值仅用于显示它(或类似的任务,如重命名)。例如,要显示 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
If this is the case, the value of the variable may be used for both control the loop anddisplay its padded value with no modification if its limits are appropriately translated. For example, to show values from 00 to 99:
如果是这种情况,变量的值可以用于控制循环和显示其填充值,如果其限制被适当地转换,则无需修改。例如,要显示 00 到 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
安东尼奥
回答by Mick
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.
这将使用 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
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 ^^
我希望这是一个很大的帮助^^
回答by Bali C
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.
本示例使用for
循环进行演示,但即使您不使用循环使用它,逻辑也是相同的。只是echo
一个0
在前面,如果数量小于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
回答by gnolt
@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