Kelly
Kelly

Reputation: 213

Windows BATCH script error - do ( was unexpected at this time

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.

Upvotes: 4

Views: 18867

Answers (1)

Endoro
Endoro

Reputation: 37569

In a batch script you have to double the %:

for /D %%G in ("%USER_DEF_VAR%\topLevelDir\*") do ( 
cd "%%~G"
...
)

Upvotes: 8

Related Questions