![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-13 00:42 |
47,811 topics / 349,897 posts / today 0 new / 48,256 members |
| DOS批处理 & 脚本技术(批处理室) » Ask for the relationship between if and goto in batch processing |
| Printable Version 1,624 / 3 |
| Floor1 pla920 | Posted 2009-12-06 23:19 |
| 新手上路 Posts 3 Credits 3 | |
|
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:) |
|
| Floor2 slore | Posted 2009-12-07 11:51 |
| 铂金会员 Posts 2,478 Credits 5,212 | |
|
```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 ``` |
|
| Floor3 pla920 | Posted 2009-12-07 20:03 |
| 新手上路 Posts 3 Credits 3 | |
|
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.) |
|
| Floor4 Hanyeguxing | Posted 2009-12-10 15:25 |
| 银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂 | |
|
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.
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |