|
martin325
银牌会员
    
积分 1582
发帖 603
注册 2006-2-20
状态 离线
|
『楼 主』:
[已结]为什么“if not exist……”语句不会跳转?
使用 LLM 解释/回答一下
如题。举一个例子:
@echo off
if not exist C:\menu.lst goto step1
grub.exe
root (hd0,0)
chainloader +1
boot
:step1
echo The "C:\menu.lst" is not exist!
echo The system is going to be shut.
shut.bat
goto quit
C盘中的menu.lst被删除,然后执行上面的批处理,结果还是不会跳转到step1去!!!
这是为什么?
请高手指点。
谢谢。
Last edited by HAT on 2008-11-13 at 17:36 ]
As the title says. Take an example:
@echo off
if not exist C:\menu.lst goto step1
grub.exe
root (hd0,0)
chainloader +1
boot
:step1
echo The "C:\menu.lst" is not exist!
echo The system is going to be shut.
shut.bat
goto quit
The menu.lst in the C drive is deleted, and then executing the above batch file, the result is still not jumping to step1!!!
Why is this?
Please expert guide.
Thank you.
Last edited by HAT on 2008-11-13 at 17:36 ]
|
|
2006-4-30 12:57 |
|
|
kcdsw
中级用户
  
积分 404
发帖 179
注册 2006-3-30
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
我也遇到过这种情况 用if语句判断的时候总是会出问题
后来我在群内高手的指点下用dir+&做的跳转
I also encountered this situation. There were always problems when using the if statement for judgment. Later, with the guidance of experts in the group, I used dir + & for the jump.
|
|
2006-4-30 13:04 |
|
|
martin325
银牌会员
    
积分 1582
发帖 603
注册 2006-2-20
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
我测试了一下,用 “if exist ……”语句是可以跳转的。
不知为什么?
I tested it, and the "if exist ……" statement can be used for jumping.
I don't know why.
|
|
2006-4-30 13:18 |
|
|
martin325
银牌会员
    
积分 1582
发帖 603
注册 2006-2-20
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
用dir+&怎么做的跳转?
怎么写的?
请举例,让我也学习学习。
How to do the jump with dir+&?
How to write it?
Give an example, let me also learn.
|
|
2006-4-30 13:24 |
|
|
martin325
银牌会员
    
积分 1582
发帖 603
注册 2006-2-20
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
已经解决了。“if not exist……”语句是可以跳转的。
出现1楼所描述的原因在于,本人把该段批处理的命名与要执行的程序名一样,结果在手工输入该批处理时,没有加上后缀“.bat”的缘故。
It has been solved. The "if not exist......" statement can jump. The reason described in the first floor is that I named this batch processing the same as the program name to be executed. As a result, when I manually entered this batch processing, I did not add the suffix ".bat".
|
|
2006-4-30 13:41 |
|
|
kcdsw
中级用户
  
积分 404
发帖 179
注册 2006-3-30
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
我上次是检测某个'参数'是文件还是目录时用了
if exist %a%\. goto b
结果他怎么都不跳转
后来用了
dir %a%\. >nul 2>nul & goto b
The last time I was detecting whether a 'parameter' was a file or a directory, I used
if exist %a%\. goto b
But it never jumped.
Later I used
dir %a%\. >nul 2>nul & goto b
|
|
2006-4-30 15:28 |
|
|
martin325
银牌会员
    
积分 1582
发帖 603
注册 2006-2-20
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
dir %a%\. >nul 2>nul & goto b 这句看不懂,能不能解释一下?
特别是“ >nul 2>nul &”
Let's break it down step by step.
First, `dir %a%\.` is a DOS command. `dir` is used to list directory contents. `%a%` is a variable. `\.` means the current directory.
Then, `>nul` redirects the standard output (the normal output of the `dir` command) to the null device, so you don't see the directory listing. `2>nul` redirects the standard error output to the null device, so any error messages are also suppressed. `&` is a command separator, allowing the next command (`goto b`) to be executed after the `dir` command has been processed. So the whole line is trying to execute the `dir` command on the directory represented by `%a%`, suppress any output and errors, and then go to the label `b`.
In English translation:
The line `dir %a%\. >nul 2>nul & goto b` is explained as follows: First, `dir %a%\.` is a DOS command to list the contents of the directory represented by the variable `%a%` in the current directory. `>nul` redirects the standard output to the null device to suppress normal output. `2>nul` redirects the standard error output to the null device to suppress error messages. `&` is a command separator, and then it jumps to the label `b` using `goto b`.
|
|
2006-4-30 17:08 |
|
|
kcdsw
中级用户
  
积分 404
发帖 179
注册 2006-3-30
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
呵呵 每个文件夹下都存在2个目录 1 是. 2是..
对不存在的目录使用dir的时候它会返回错误值
而我的目的是得知它存在不存在 所以不论正确与否 dir的执行结果我是不需要显示出来的
我只需要用& 来取它的结果
Hehe, there are 2 directories in each folder: 1 is ., and 2 is ..
When using dir on a non-existent directory, it will return an error value.
And my purpose is to know whether it exists or not, so I don't need to display the execution result of dir regardless of whether it is correct or not.
I just need to use & to get its result.
|
|
2006-4-30 18:15 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
用dir C:\menu.lst >nul 2>nul && goto step1吧。
dir %a%\. >nul 2>nul & goto b这句的意思是:
查找名为%a%的目录,然后跳转到标号b处。这句执行完后不管是否存在目录%a%都会跳转到b处,所以应该和你的要求有所偏差。
>nul 2>nul表示dir命令产生的信息不在屏幕上显示,具体详细解释可翻一翻论坛老贴。
dir %a%\. 表示查找%a%目录下的.目录,在批处理中,.表示同级目录,".."表示上一级目录,所以dir %a%就是表示查找%a%目录。
补充:
1,此命令是根据dir是否正确执行来跳转,dir是可以直接用来查找目录的,所以可以直接写成dir %a% >nul 2>nul & goto b
2,考虑到命令行中可以能碰到路径包含空格的情况,一般在调用变量的时候两边加上""“号可以防止出错: dir "%a%" >nul 2>nul && goto Label
Use dir C:\menu.lst >nul 2>nul && goto step1.
The meaning of the sentence dir %a%\. >nul 2>nul & goto b is: Find the directory named %a%, and then jump to the label b. After this sentence is executed, it will jump to b regardless of whether the directory %a% exists, so it should be somewhat deviated from your requirement.
>nul 2>nul means that the information generated by the dir command is not displayed on the screen. For specific detailed explanations, you can turn over the old posts in the forum.
dir %a%\. means to find the. directory under the %a% directory. In batch processing,. represents the same-level directory, and ".." represents the upper-level directory, so dir %a% means to find the %a% directory.
Supplementary:
1. This command jumps according to whether the dir is executed correctly. dir can be directly used to find the directory, so it can be directly written as dir %a% >nul 2>nul & goto b.
2. Considering that there may be cases where the path contains spaces in the command line, generally adding "" around the variable when calling can prevent errors: dir "%a%" >nul 2>nul && goto Label
|
|
2006-4-30 18:21 |
|
|
martin325
银牌会员
    
积分 1582
发帖 603
注册 2006-2-20
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
dir C:\menu.lst >nul 2>nul && goto step1
这里有两个 &&
分别表示什么意思?
In the Windows command line, `&&` is a logical operator. The first `dir C:\menu.lst >nul 2>nul` is a command that tries to list the file `menu.lst` in the `C:` drive. The `>nul` redirects the standard output to the null device, and `2>nul` redirects the standard error output to the null device. The `&&` means that if the preceding command ( `dir C:\menu.lst >nul 2>nul` ) succeeds (returns an exit code of 0), then the next command ( `goto step1` ) will be executed. So the first `&&` connects the `dir` command and the `goto` command, and the second `&&` is the same logical connection function here. So the translation is: In the Windows command line, `&&` is a logical operator. The first `dir C:\menu.lst >nul 2>nul` is a command that attempts to list the file `menu.lst` on the `C:` drive. `>nul` redirects standard output to the null device, and `2>nul` redirects standard error output to the null device. `&&` means that if the preceding command ( `dir C:\menu.lst >nul 2>nul` ) succeeds (returns an exit code of 0), then the next command ( `goto step1` ) will be executed. So the first `&&` connects the `dir` command and the `goto` command, and the second `&&` has the same logical connection function here.
|
|
2006-4-30 21:52 |
|
|
220110
荣誉版主
      
积分 718
发帖 313
注册 2005-9-26
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
命令1 & 命令2 :单个&号表示,无论前面命令1执行成功与否,命令2照常执行.
命令1 && 命令2 :表示,只有命令1执行成功,并成功返回"0"的参数给命令2时,命令2才会继续.
命令1 | 命令2 与 命令1 || 命令2 的用法类似.
Command 1 & Command 2: The single & sign means that regardless of whether command 1 is executed successfully or not, command 2 is executed as usual.
Command 1 && Command 2: It means that command 2 will continue only if command 1 is executed successfully and successfully returns a parameter "0" to command 2.
The usage of Command 1 | Command 2 is similar to that of Command 1 || Command 2.
|
|
2006-5-1 23:05 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
command1 | command2
表示把command1的输出当作command2的参数。
command1 || command2
表示若command1执行不成功才执行command2,反之则不执行。
command1 | command2
Means taking the output of command1 as an argument for command2.
command1 || command2
Means that command2 is executed only if command1 is not successful; otherwise, it is not executed.
|
|
2006-5-2 01:06 |
|
|
gxp2005
新手上路

积分 2
发帖 1
注册 2006-5-5
状态 离线
|
|
2006-5-5 11:03 |
|
|
fan927
初级用户
 
积分 82
发帖 31
注册 2006-5-23
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
其实一些"是否"判断用管道符号很方便,用if反而复杂化,一些个人观点.
dir C:\menu.lst &&grub.exe&root (hd0,0)&chainloader +1&boot||echo The "C:\menu.lst" is not exist!&echo The system is going to be shut.
不对的地方请高手指正
Actually, some "whether" judgments are very convenient with the pipe symbol, and using if complicates it, some personal views.
dir C:\menu.lst &&grub.exe&root (hd0,0)&chainloader +1&boot||echo The "C:\menu.lst" is not exist!&echo The system is going to be shut.
Please correct the incorrect places for the experts.
|
|
2006-5-24 13:03 |
|
|
nafan
初级用户
 
积分 22
发帖 21
注册 2008-11-13
状态 离线
|
|
2008-11-13 17:10 |
|