Very simple, I guess... I need to get a usable variable by adding leading zeros to the loop index variable (%%i) below.
@echo off
for /L %%i in (1, 1, 5) do (
echo %%i
rem How to create a variable j here as a
rem result of adding leading zeros to %%i? (001, 002, 003 etc.)
)
pause
How? I've tried the following, but I can't get the value out of the %%i variable inte the var_ at a...
@echo off & setlocal enableextensions
for /L %%i in (1, 1, 5) do (
echo %%i
set var_=00000%%i
set var_=%var_:~-5%
echo %var_%
)
pause
If the variable in which you want to add leading zeros contains only numeric values, we can simply use Zw. d format. In this case, we made length of the newly transformed variable as 6.
Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.
Prefix the string with zeros and then take the desired count of characters from the right side:
@echo off
set count=5
setlocal EnableDelayedExpansion
for /L %%i in (1, 1, %count%) do (
set "formattedValue=000000%%i"
echo !formattedValue:~-3!
)
Outputs:
001
002
003
004
005
Using the setlocal enabledelayedexpansion
, the solution is this:
@echo off
setlocal ENABLEDELAYEDEXPANSION
set count=5
for /L %%i in (1, 1, %count%) do (
echo %%i
set j=00%%i
rem to display intermediate values inside loop, surround with !
echo !j!
)
endlocal
Here is a good reference: http://blog.crankybit.com/why-that-batch-for-loop-isnt-working/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With