f1rstsurf
f1rstsurf

Reputation: 647

Use loop with multiple array

I try to create a simple batch file to install Windows CRM. To do it i created 2 arrays which contains URI and name of the downloaded files.

Now, I want to iterate into theses array to download my files. I proceed this way :

setlocal EnableDelayedExpansion

set name=(
SSCERuntime-ENU-x86.msi
SSCERuntime-ENU-x64.msi
msoidcli_64.msi
wllogin_64.msi
ReportViewer.exe
CRM2011-Client-FRA-i386.exe
)

set link=(
"http://go.microsoft.com/fwlink/p/?LinkId=147327"
"http://go.microsoft.com/fwlink/p/?LinkId=147326"
"http://go.microsoft.com/fwlink/p/?LinkId=221500"
"http://go.microsoft.com/fwlink/?LinkId=194722" 
"http://go.microsoft.com/fwlink/?LinkId=193386&clcid=0x409"
)

for %%G in %name% do echo %%G = !%%G!

SET i = 1
(for %%a in (%name%) do (
  if exist \\srv-dc22\netlogon\CRM\%%G (
  echo %%G : Already donwnloaded
)else (
  echo Downloading %%G
  powershell -command "& { iwr %link[i]% -OutFile %%G }"
  SET /A i+=1
  )
 )
)

But even if i success to get the value for "name" , i do not success to iterate my "link" array.

What's wrong?

Upvotes: 0

Views: 57

Answers (1)

Aacini
Aacini

Reputation: 67206

There are several points involved in your question...

In Batch files there is NOT a predefined support for arrays. You may still emulate the array management in Batch files making good use of the fact that the names of Batch variables may include almost any special character. For example set var=123 is the same (from Batch file processing point of view) than set array[1]=123. This way, is up to you to successfully emulate the array management in Batch files. You may read further details on this point at: Arrays, linked lists and other data structures in cmd.exe (batch) script

@echo off
setlocal EnableDelayedExpansion

rem Define the *list* named "name"
set name=SSCERuntime-ENU-x86.msi SSCERuntime-ENU-x64.msi msoidcli_64.msi wllogin_64.msi ReportViewer.exe CRM2011-Client-FRA-i386.exe

rem Define the *array* named "link"
set "link[1]=http://go.microsoft.com/fwlink/p/?LinkId=147327"
set "link[2]=http://go.microsoft.com/fwlink/p/?LinkId=147326"
set "link[3]=http://go.microsoft.com/fwlink/p/?LinkId=221500"
set "link[4]=http://go.microsoft.com/fwlink/?LinkId=194722" 
set "link[5]=http://go.microsoft.com/fwlink/?LinkId=193386&clcid=0x409"

for %%G in (%name%) do echo %%G

SET i=1
for %%G in (%name%) do (
   if exist \\srv-dc22\netlogon\CRM\%%G (
      echo %%G : Already donwnloaded
   ) else (
      echo Downloading %%G
      for %%i in (!i!) do powershell -command "& { iwr !link[%%i]! -OutFile %%G }"
      SET /A i+=1
   )
)

In previous code, the definition of the "link" array could be done in a for command in a simpler (and less ugly) way; however, the inclusion of the ? special character in the values avoids to use they as string literals in a for.

Upvotes: 2

Related Questions