Windows batch comma delimited list in for loop
Solution 1
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"
)
Solution 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"
)
jlengrand
Developer Advocate @Adyen. I create 'islands' where engineers are the heroes. Podcast @JuliensTech. #jvm #fp #elm #java https://lengrand.fr/
Updated on July 27, 2022Comments
-
jlengrand over 1 year
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!