Batch file goto not working in for loop

2019-05-31 09:58发布

I have a for loop in my batch file that loops through folders. I want to skip particular folders. I can do this with an IF statement, but would prefer a GOTO like I'm showing below.

for /d %%F in (*) do (
 if /i "%%F"=="Archive" goto nextFolder
 REM do stuff here
:nextFolder
)

But the above is giving me errors:

) was unexpected at this time

2条回答
疯言疯语
2楼-- · 2019-05-31 10:59

This won't work - you can't jump into a control-flow construct and expect everything to be fine.

Please take a look at (Windows batch) Goto within if block behaves very strangely for a good discussion of why this is a terrible idea.

查看更多
小情绪 Triste *
3楼-- · 2019-05-31 11:01

Rather than using GOTO, you could use NOT to exclude a folder or folders.

for /d %%F in (*) do (
   if /i NOT "%%F"=="ARCHIVE"
       REM do stuff here
)
查看更多
登录 后发表回答