-1

Hi I'm trying to loop and pad some variables with 0 in a batch script. I've looked up on stackoverflow to do both but have some trouble weith percents and double percents.

The following fails to pad it with 0s. Where should I add percents here?

for /L %%i in (1,1,10) do (
    set "i=0%i%"
    set "i=%i:~-2%"
    echo "%%i"
)
simonzack
  • 19,729
  • 13
  • 73
  • 118

1 Answers1

1

if you change a variable and want to use it in the same block, you have to use delayed expansion:

setlocal enabledelayedexpansion
for /L %%i in (1,1,10) do (
    set "i=0%%i"
    set "i=!i:~-2!"
    echo "!i!"
)
Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • thanks but no need for the downvote theres no way i could have googled this, i did try – simonzack Jun 13 '16 at 16:08
  • that wasn't me. Easy to google, if you know the right search term - if you don't, you're lost. – Stephan Jun 13 '16 at 16:10
  • if you search for something like "variable for loop in batch file" you'll get a lot of answers with delayed expansion and the use of `!` instead of `%` – phuclv Jun 13 '16 at 16:16
  • @LưuVĩnhPhúc yes - as soon as you know what the problem is, you will easily find the answers. Problem here was masked by confusion with `for` variables and "normal" variables. – Stephan Jun 13 '16 at 16:19