简体   繁体   中英

Concatenating strings to iteration numbers in batch

I have the following batch script:

SETLOCAL ENABLEDELAYEDEXPANSION
set "base_name=distribution"
set "dir_net_name=dir_net"


FOR %%? IN (1,1,10) DO (
set "itnum=%%?"
set "name=%dir_net_name%\%base_name%_%itnum%"
echo %name%
)
pause

I have the following problems: (1) The loop iterates over 1, 1, and 10 rather than 1 through 10 (2) I want to concatenate dir_net_name + \\ + base_name + _ + itnum, but itnum does not show (3) name does not show, echo does nothing

I have tried this over and over again, but still not working!

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set "base_name=distribution"
set "dir_net_name=dir_net"

FOR /L %%i IN (1,1,10) DO (
set "name=%dir_net_name%\%base_name%_%%i"
echo !name!
)

pause
  • ad 1) see help for for details on /L switch.
  • ad 2) %itnum% is redundant here. If you still need it use it as !itnum! .
  • ad 3) %name% is evaluated before FOR loop starts, use !name! instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM