China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-13 01:39
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Ask for the relationship between if and goto in batch processing View 1,625 Replies 3
Original Poster Posted 2009-12-06 23:19 ·  中国 上海 电信
新手上路
Credits 3
Posts 3
Joined 2009-11-08 11:04
16-year member
UID 154385
Gender Male
Status Offline
I'm a novice. I read a tutorial about the if statement, which mentions that when there are multiple commands and multiple variables in an if statement, the commands and variables need to be guided by the goto statement! Otherwise, the if statement will only execute the first command and variable.
-------------------------------------------------The example is as follows:
@echo on
set /p num=Please enter the operation you want to be confident about:
if "%num%"=="1" (
set /p a=Please enter content:
set /p b=Please enter file name:
set /p c=Please enter the file name after copying:
goto a
:a
echo %a% >%b%.txt
goto b
:b
copy %b%.txt c:\%c%.txt
)
pause
set a=
set b=
set c=
set num=
-----------------------------------------------------------------
I just want to ask the master how this goto is called. Isn't it said that batch processing commands are executed one by one from top to bottom? If so, what is the meaning of these two goto statements? Without the goto statement, the generated txt file has no title and exists as .txt. That's the situation mentioned above. I don't understand programming very much. I hope those who know can inform me. Thank you here first:)
Floor 2 Posted 2009-12-07 11:51 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
```batch
@echo off
set num=1
if "%num%"=="1" (
set /p a1=请输入a1:
set /p b1=请输入b1:
goto label1
:label1
echo %a1%
echo %b1%
)
pause

if "%num%"=="1" (
set /p a2=请输入a2:
set /p b2=请输入b2:
echo %a2%
echo %b2%
)
pause

rem 启用延时变量
setlocal EnableDelayedExpansion
if "%num%"=="1" (
set /p a3=请输入a3:
set /p b3=请输入b3:
echo !a3!
echo !b3!
)
pause


Then the commands and variables need the goto statement to be guided!
This goto command is not to achieve logical jump, logically it still has to execute to echo
This is a mechanism problem of cmd processing variables.
Only by goto or enabling variable delay can we get the variables inside the if/for () block
```
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
Floor 3 Posted 2009-12-07 20:03 ·  中国 上海 静安区 电信
新手上路
Credits 3
Posts 3
Joined 2009-11-08 11:04
16-year member
UID 154385
Gender Male
Status Offline
Does the meaning of the second floor mean that goto in the if/for() statement only plays the role of guiding variables, and there is no logical jump role.
I want to ask a few more questions. Is this the case, only occurring in if and for statements? If so, can all be solved by guiding with goto statements to solve such similar problems, and are there other methods (such as the method of adding setlocal EnableDelayedExpansion and double! given by the second floor)? Thank you (still a novice, just ignorant, please forgive me a lot.)
Floor 4 Posted 2009-12-10 15:25 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
This can be understood in this way. Inside if or for, dynamic capture of variables generally requires variable delay. As for using goto or setlocal EnableDelayedExpansion, in principle, both are acceptable, but for example, if 10 variables need to be captured dynamically, then 10 gotos are needed at this time, which will increase the amount of code used. It is not as convenient as a setlocal EnableDelayedExpansion, and it is also more clear to the person reading the code. In actual use, it is not recommended to use goto inside if or for.
Forum Jump: