2014-03-07 37 views
0

我希望在我的批處理文件中有for循環來迭代2個數組。在我心中的僞代碼如下所示:批處理文件對於具有2個數組的循環

for each i in array1 
print i 
print array2[x++] 

array1和array2將具有相同的大小。

我能在批處理文件中達到相同的結果嗎? 我目前有以下代碼。

for %%i in %APP_LIST1% DO (
    %appcmd% add app /site.name:%siteName% /path:/%%i /physicalPath:"d:\Apps\%%i" 
) 

我想在同一個for循環中使用%APP_LIST2%(aka array2)。

請幫忙!

+0

「APP_LIST1」的格式是什麼?你可以發佈一些嗎? – unclemeat

+0

你只想爲兩個數組做同樣的事情,還是你想要做一些不同於'APP_LIST2'中的值? – unclemeat

回答

1

恐怕我真的不明白你對第二陣列的擔心。如果您可以訪問一個陣列,則可以以相同的方式訪問任意數量的陣列...

請注意,數組是「可以通過運行時計算的索引選擇的數據項集合」時間「,如Wikipedia中所定義的。這樣,您可以使用與第一個陣列中使用的相同索引訪問第二個;這應該適用於你的情況,因爲「array1和array2將具有相同的大小」。

例如:

@echo off 
setlocal EnableDelayedExpansion 

rem Create first array 
set i=0 
for %%a in (apple orange pear) do (
    set /A i+=1 
    set fruit[!i!]=%%a 
) 

rem Create second array 
set i=0 
for %%a in (red green blue) do (
    set /A i+=1 
    set color[!i!]=%%a 
) 

rem Access both arrays at same time 
for /L %%i in (1,1,3) do (
    echo Fruit: !fruit[%%i]!, color: !color[%%i]! 
) 

對於批處理文件上陣列管理進一步的細節,請參見:this post