![]() |
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-10 11:03 |
47,811 topics / 349,897 posts / today 0 new / 48,255 members |
| DOS批处理 & 脚本技术(批处理室) » [Closed] Why doesn't the "if not exist……" statement jump? |
| Printable Version 2,221 / 14 |
| Floor1 martin325 | Posted 2006-04-30 12:57 |
| 银牌会员 Posts 603 Credits 1,582 | |
|
As the title says. Take an example:
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 ] |
|
| Floor2 kcdsw | Posted 2006-04-30 13:04 |
| 中级用户 Posts 179 Credits 404 | |
|
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.
|
|
| Floor3 martin325 | Posted 2006-04-30 13:18 |
| 银牌会员 Posts 603 Credits 1,582 | |
|
I tested it, and the "if exist ……" statement can be used for jumping.
I don't know why. |
|
| Floor4 martin325 | Posted 2006-04-30 13:24 |
| 银牌会员 Posts 603 Credits 1,582 | |
|
How to do the jump with dir+&?
How to write it? Give an example, let me also learn. |
|
| Floor5 martin325 | Posted 2006-04-30 13:41 |
| 银牌会员 Posts 603 Credits 1,582 | |
|
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".
|
|
| Floor6 kcdsw | Posted 2006-04-30 15:28 |
| 中级用户 Posts 179 Credits 404 | |
|
The last time I was detecting whether a 'parameter' was a file or a directory, I used
But it never jumped. Later I used |
|
| Floor7 martin325 | Posted 2006-04-30 17:08 |
| 银牌会员 Posts 603 Credits 1,582 | |
|
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`. |
|
| Floor8 kcdsw | Posted 2006-04-30 18:15 |
| 中级用户 Posts 179 Credits 404 | |
|
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. |
|
| Floor9 3742668 | Posted 2006-04-30 18:21 |
| 荣誉版主 Posts 718 Credits 2,013 | |
|
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 |
|
| Floor10 martin325 | Posted 2006-04-30 21:52 |
| 银牌会员 Posts 603 Credits 1,582 | |
|
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.
|
|
| Floor11 220110 | Posted 2006-05-01 23:05 |
| 荣誉版主 Posts 313 Credits 718 | |
|
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. |
|
| Floor12 3742668 | Posted 2006-05-02 01:06 |
| 荣誉版主 Posts 718 Credits 2,013 | |
|
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. |
|
| Floor13 gxp2005 | Posted 2006-05-05 11:03 |
| 新手上路 Posts 1 Credits 2 | |
|
I've learned another trick again
|
|
| Floor14 fan927 | Posted 2006-05-24 13:03 |
| 初级用户 Posts 31 Credits 82 | |
|
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. |
|
| Floor15 nafan | Posted 2008-11-13 17:10 |
| 初级用户 Posts 21 Credits 22 | |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |