这对你有用吗?
setlocal enabledelayedexpansion
set inc=0
:heck
set /a "inc+=1"
if defined CORE%inc% (
set "HOSTS=%HOSTS% !CORE%inc%!
goto heck
)
rem remove leading space from %HOSTS%
set HOSTS=%HOSTS:~1%
这是一个更完整的测试脚本。
@echo off
setlocal enabledelayedexpansion
set CORE1=foo
set CORE2=bar
set CORE3=baz
set CORE4=qux
set CORE5=quux
set CORE6=corge
set CORE7=grault
set CORE8=garply
set CORE9=waldo
set CORE10=fred
set CORE11=plugh
set CORE12=xyzzy
set CORE13=thud
set inc=0
:heck
set /a "inc+=1"
if defined CORE%inc% (
set "HOSTS=%HOSTS% !CORE%inc%!
goto heck
)
rem remove leading space from %HOSTS%
set HOSTS=%HOSTS:~1%
echo %HOSTS%
示例输出:
C:\Users\me\Desktop>test
foo bar baz qux quux corge grault garply waldo fred plugh xyzzy thud