4

I try to run a simple batch script as shown below. Basically I want to visit each sub-directory in a given directory and run a gmake command.

echo %USER_DEF_VAR%
cd %USER_DEF_VAR%\topLevelDir
for /D %G in ("%USER_DEF_VAR%\topLevelDir\*") do ( 
cd %G
gmake clean
gmake 
)

If I run this script on a command line, it works fine. But when I put this in a .bat script it throws me an error saying

do ( was unexpected at this time.

I have tried removing spaces wherever possible as suggested here - nested For loop in batch file error: Do was unexpected at this time

I'm still facing the same issue.

Community
  • 1
  • 1
Kelly
  • 213
  • 1
  • 6
  • 21
  • I had a [similar issue](http://stackoverflow.com/questions/6134137/windows-command-works-from-command-prompt-but-not-in-cmd-file-any-idea-why) and it turned out that I needed two percents in the batch file. Try that. – Mr Moose May 27 '13 at 06:16
  • Yep worked for me too :) , Thanks Endoro & Mr Moose :) – Kelly May 27 '13 at 06:27

1 Answers1

8

In a batch script you have to double the %:

for /D %%G in ("%USER_DEF_VAR%\topLevelDir\*") do ( 
cd "%%~G"
...
)
Endoro
  • 37,015
  • 8
  • 50
  • 63