0

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?

f1rstsurf
  • 637
  • 1
  • 6
  • 22
  • You need to append _space_ + `^` to every line in your multi-line `set` commands (except after `)`) to tell the command interpreter that the command line is continued; otherwise every line is interpreted as a separate command; the _space_ serves as delimiter (needed by `for` later); if any of the items contain white-spaces or special characters, enclose each in `""`; expand the `for` variable using `~` like `%%~a` to remove the `""` afterwards... – aschipfl Mar 10 '16 at 15:05
  • I tried what you gave to me , but it seems not functionnal. i still not have the link – f1rstsurf Mar 10 '16 at 15:33

1 Answers1

2

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.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108