2

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!

Community
  • 1
  • 1
jlengrand
  • 12,152
  • 14
  • 57
  • 87

3 Answers3

11

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"
)
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Thanks it does work that way. In order to make it perfect, do you know if there is a way to be able to insert comment too ? I tried "7zip.install :: comment" but it doesn't really like it :s – jlengrand Jun 20 '13 at 08:07
  • No, there can't be a comment inside the parentheses `()`, also `REM` doesn'n work, there is all interpreted as a valid string. Put your comment in the line before `for %%i in (`. – Endoro Jun 20 '13 at 08:11
  • 5
    You can use _percent comments_ like `"7zip.install" %= one comment%`. This is effectivly a variable expansion for an undefined variable – jeb Jun 20 '13 at 08:13
  • @jeb thank you - I doesn't like this, so I had forgotten it :) – Endoro Jun 20 '13 at 08:17
2

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"
)
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

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
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103