Jump to content

Padding zeroes in a batch file

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
I never thought it would come to this...but I need help with a simple batch script. It's been ages since I dabbled with batch files in DOS, so I'm a bit rusty, but it seems to me that this should be right.

for /L %%X in (9,1,10) do (

echo %%X

set p=%%X

echo %p%

if %%X lss 10 set p=0%p%

)

Yet, it prints different results whenever I run it. I have a suspicion that the p in my script isn't actually set until the batch file has terminated, so I get the old value of p. But if I want to zero pad the values in a for loop, then how else am I supposed to do it?

#2
kaheidt

kaheidt

    Newbie

  • Members
  • Pip
  • 1 posts
Don't know if you still need this or not, but I think I achieved what you needed by doing this.

ECHO OFF

for /L %%X in (9,1,10) do CALL :process %%X

pause

goto :skipprocess

:process

set p=%1

if %1 lss 10 set p=0%1

echo %p%

:skipprocess