如何跨 ENDLOCAL 返回值数组

Jer*_*ryB 2 arrays return batch-file

我有两个可变长度的值数组,TargetName[] 和 TargetCpu[],我需要跨 ENDLOCAL 边界返回它们。我已尝试以下操作,但仅返回第一个数组上的第一个值。

for /L %%i in (0,1,%MaxIndex%) do (
    for /f "delims=" %%j in (""!TargetName[%%i]!"") do (
        for /f "delims=" %%k in (""!TargetCpu[%%i]!"") do (
            endlocal
            set TargetName[%%i]=%%j
            set TargetCpu[%%i]=%%k
        )
    )
)
Run Code Online (Sandbox Code Playgroud)

下面是返回值的打印。

Number Targets: 3
TargetName[0]: "Computer1"
TargetCpu[0] : "x64"
TargetName[1]: "!TargetName[1]!"
TargetCpu[1] : "!TargetCpu[1]!"
TargetName[2]: "!TargetName[2]!"
TargetCpu[2] : "!TargetCpu[2]!"
Run Code Online (Sandbox Code Playgroud)

我已经阅读了我能找到的所有内容,但我尝试过的所有内容都不适用于可变长度数组。

Aac*_*ini 6

@echo off
setlocal

set "MaxIndex=6"
call :CreateArrays
set TargetName
set TargetCPU
goto :EOF


:CreateArrays

setlocal EnableDelayedExpansion
for /L %%i in (1,1,%MaxIndex%) do (
   set /A TargetName[%%i]=!random!, TargetCpu[%%i]=!random!
)

rem Return the arrays to the calling scope
set "currentScope=1"
for /F "delims=" %%a in ('set TargetName[ ^& set TargetCPU[') do (
   if defined currentScope endlocal
   set "%%a"
)
exit /B
Run Code Online (Sandbox Code Playgroud)