0

I am having a list of id number like the following:

123
4456
657
23
199

i would like to add padding zero to it so that it will end up with 5 digit with result like this:

00123
04456
00657
00023
00199

I have looked up online but couldn't really find a solution for .bat file. I have tried something like the following:

set num_max=5
set my_num=123

set "my_num=00000%my_num%"
set "mynum=!my_num:!~-%num_max%!"
echo %my_num%

but the reuslt becomes something like 00000123. I have a loop to read all the input value

for /f "tokens=1" %%a in (.\config.ini) do (
echo %%a
)
``` and the %%a is the value that I would like to add the padding zero to.
4

1 回答 1

-1

如果您想继续学习一些 PowerShell,您可以在batch-file. 如果您使用的是受支持的 Windows 系统,则可以使用 PowerShell。

set "num_max=5"
set "my_num=123"
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command "'%my_num%'.PadLeft(%num_max%,'0')"') DO (SET "my_num=%%~A")
ECHO my_num is %my_num%

当然,如果脚本是用 PowerShell 编写的,那就更容易了。

$num_max = 5
$my_num = 123
$my_left_padded_num = "{0:$('0' * $num_max)}" -f $my_num 
$my_left_padded_num
于 2021-02-09T16:20:36.810 回答