Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

batch file - why both goto labels executes when both folder1 & 2 exist

For below batch file, When both or any one folder NOT exist then ONLY :notfound label execute which is correct, but when both folder exist, then both :bothfound and :notfound labels execute.

How we can run ONLY execute :bothfound when both folder Temp1 and Temp2 exist?

@ECHO OFF

set "folder1=C:Temp1"
set "folder2=C:Temp2"

IF EXIST %folder1% IF EXIST %folder2% goto bothfound
goto notfound


:bothfound
echo Both folders exist.

:notfound
echo either one or both folder not exist.

echo Done.
pause
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Same problem as your other question.

Batch files process top to bottom. If a goto is not employed to skip lines, it will execute the next line. These are not methods like you think of in a modern programming language; the start of a new label doe not imply an end to the prior label.

:bothfound
echo Both folders exist.
goto end

:notfound
echo either one or both folder not exist.
goto end

:end
echo Done.
pause

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...