-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"
)
1

1 Answer 1

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!"
)
Sign up to request clarification or add additional context in comments.

4 Comments

thanks but no need for the downvote theres no way i could have googled this, i did try
that wasn't me. Easy to google, if you know the right search term - if you don't, you're lost.
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 %
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.