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-10 12:08
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Closed] Why doesn't the "if not exist……" statement jump? View 2,222 Replies 14
Original Poster Posted 2006-04-30 12:57 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 1,582
Posts 603
Joined 2006-02-20 20:26
20-year member
UID 50690
Status Offline
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 ]
Floor 2 Posted 2006-04-30 13:04 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
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.
Floor 3 Posted 2006-04-30 13:18 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 1,582
Posts 603
Joined 2006-02-20 20:26
20-year member
UID 50690
Status Offline
I tested it, and the "if exist ……" statement can be used for jumping.
I don't know why.
Floor 4 Posted 2006-04-30 13:24 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 1,582
Posts 603
Joined 2006-02-20 20:26
20-year member
UID 50690
Status Offline
How to do the jump with dir+&?

How to write it?

Give an example, let me also learn.
Floor 5 Posted 2006-04-30 13:41 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 1,582
Posts 603
Joined 2006-02-20 20:26
20-year member
UID 50690
Status Offline
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".
Floor 6 Posted 2006-04-30 15:28 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
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
Floor 7 Posted 2006-04-30 17:08 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 1,582
Posts 603
Joined 2006-02-20 20:26
20-year member
UID 50690
Status Offline
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`.
Floor 8 Posted 2006-04-30 18:15 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
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.
Floor 9 Posted 2006-04-30 18:21 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
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
Floor 10 Posted 2006-04-30 21:52 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 1,582
Posts 603
Joined 2006-02-20 20:26
20-year member
UID 50690
Status Offline
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.
Floor 11 Posted 2006-05-01 23:05 ·  中国 广东 云浮 新兴县 电信
荣誉版主
★★★
Credits 718
Posts 313
Joined 2005-09-26 00:00
20-year member
UID 42844
Gender Male
Status Offline
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.
Floor 12 Posted 2006-05-02 01:06 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
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.
Floor 13 Posted 2006-05-05 11:03 ·  中国 山东 烟台 电信
新手上路
Credits 2
Posts 1
Joined 2006-05-05 11:00
20-year member
UID 54962
Status Offline
I've learned another trick again
Floor 14 Posted 2006-05-24 13:03 ·  中国 广东 深圳 罗湖区 电信
初级用户
Credits 82
Posts 31
Joined 2006-05-23 00:33
20-year member
UID 55824
Status Offline
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.
Floor 15 Posted 2008-11-13 17:10 ·  中国 北京 鹏博士BGP
初级用户
Credits 22
Posts 21
Joined 2008-11-13 13:44
17-year member
UID 130844
Gender Male
Status Offline
Forum Jump: