jlengrand
jlengrand

Reputation: 12807

Windows batch comma delimited list in for loop

I know there are a lot of related questions on SO, like this one or this one, but for some reason I couldn't get any of those working so far.

I am trying to create a batch file to install several pieces of software in a row :

Here is my batch file

set packages=(7zip.install, ^ :: for compression
notepadplusplus.install, ^ :: file editor
Firefox, ^
putty, ^
mysql -Version 5.5.30, ^
postgresql)

for %%i in %packages% do ( 
::echo %%i
cinst %%i
)

Everything works fine but for the mysql part. The space is actually taken as a delimiter, which means that I get

E:\>(echo 7zip.install )
7zip.install

E:\>(echo notepadplusplus.install )
notepadplusplus.install

E:\>(echo Firefox )
Firefox

E:\>(echo putty )
putty

E:\>(echo mysql )
mysql

E:\>(echo -Version )
-Version

E:\>(echo 5.5.30 )
5.5.30

E:\>(echo postgresql )
postgresql

What I would like to end up with is

E:\>(echo mysql -Version 5.5.30 )
 mysql -Version 5.5.30

The trick is that I want to keep my list on top of the script with one element per line, and have the possibility to insert a comment to be clear about what happens.

Is there any way to do this?

I am way more used to linux bash, and I must say I am a bit lost with the windows notations.

Thanks!

Upvotes: 2

Views: 10365

Answers (3)

Aacini
Aacini

Reputation: 67206

Although somewhat verbose, this method allows you to insert comments in each line:

set "packages=           7zip.install"                  // for compression
set "packages=%packages% notepadplusplus.install"       // file editor
set "packages=%packages% Firefox"
set "packages=%packages% putty"
set "packages=%packages% "mysql -Version 5.5.30""       // use nested quotes here
set "packages=%packages% postgresql"

for %%i in (%packages%) do (
   echo %%~i
   REM cinst "%%~i"
)

Upvotes: 2

Loïc MICHEL
Loïc MICHEL

Reputation: 26120

for /F "tokens=*" %%i in %packages% ?

just example for mysql :

C:\temp>for /F "tokens=1,2,3,4,5 delims=," %i in ("%packages%") do echo %l

C:\temp>echo mysql -Version 5.5.30
mysql -Version 5.5.30

but I had to rewrite your var definition:

set packages=7zip.install,notepadplusplus.install,putty,mysql -Version 5.5.30,postgresql

Upvotes: 0

Endoro
Endoro

Reputation: 37569

try this:

@ECHO OFF &setlocal

for %%i in (
    "7zip.install"
    "notepadplusplus.install"
    "Firefox"
    "putty"
    "mysql -Version 5.5.30"
    "postgresql"
        ) do (
    echo %%i
    cinst "%%~i"
)

Upvotes: 11

Related Questions