mnabil
mnabil

Reputation: 745

Loop over a list items in a batch script

I am trying to check if a set of directory exists or not. I am using a batch script.

I use "for" loop to loop over the list of the directories and checks if this directory exist or not:

set list=%HOME%\bin ^
%HOME%\tmp

for %%A in (%list%) do (
    if not exist %%A (
       echo %%A
    )
)

The problem is that I get an error: 'C:\mn\home\bin' is not recognized as an internal or external command, operable program or a batch file.

How to make the for loop avoid executing the items of the list?

Thanks

Upvotes: 0

Views: 2342

Answers (1)

user6811411
user6811411

Reputation:

Put the list items in one line,
if they might contain spaces enclose in double quotes: set list="%HOME%\bin" "%HOME%\tmp"
separate items by space, comma, semicolon, tab or equal sign.

@Echo off
Set "Home=C:\mn\home"
set list="%HOME%\bin\",^
 "%HOME%\tmp\"

for %%A in (%list%) do (
    if not exist "%%~A" (
       echo doesn't exist %%A
    )
)

Sample output:

> SO_5053242.cmd
doesn't exist "C:\mn\home\bin\"
doesn't exist "C:\mn\home\tmp\"

It might be easier to put your items into the for directly:

@Echo off
Set "Home=C:\mn\home"

for %%A in (
"%HOME%\bin\"
"%HOME%\tmp\"
"%HOME%\blahtmp\"
) do (
    if not exist "%%~A" (
       echo doesn't exist %%A
    )
)

Upvotes: 2

Related Questions