Monday, February 2, 2009

Batch File Note to Self on Using "For"

I finally realized why some of my For loops simply weren't working in many of my batch files. It turns out that Batch is not parsed like HTML as I expected for some reason, so where I was using:


For /f %%a In ('dir /b /a:d') Do
(
Echo %%a is a folder.
)

I should have been coding:


For /f %%a In ('dir /b /a:d') Do (
Echo %%a is a folder.
)

Do you see the difference? It's that last opening parentheses, it has to be on the same line as the "For" statement. I think it might have something to do with the For statement being technically a one line statement with a possible line continuation if Parenthesis are included and since they are optional if it opens on the next line it must be a part of some other statement.