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-02 02:26
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Regarding the cmd to judge whether a disk partition exists View 1,754 Replies 9
Original Poster Posted 2006-09-10 01:38 ·  中国 江苏 扬州 电信
中级用户
Credits 203
Posts 44
Joined 2003-08-14 00:00
22-year member
UID 8600
Gender Male
Status Offline
There are a few issues with your code. Here's the corrected version:

```batch
@echo off
if exist z:\ (
echo You have drive Z:
) else (
echo You don't have drive Z:
)
pause
```

The problem with your original code is that simply doing `z:` doesn't properly check if the drive exists in a reliable way. The `if exist` command is a better and more straightforward way to check if a drive or directory exists.
Floor 2 Posted 2006-09-10 02:13 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Because CMD executes in top-to-bottom order - if there are no jump statements like goto in the middle, so when it executes the echo You don't have drive Z: sentence, because there are no jump statements, the program will continue to go down and execute the content of :right.

The solution is: add a line of pause && goto :eof above :right.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 3 Posted 2006-09-10 04:48 ·  中国 江苏 扬州 电信
中级用户
Credits 203
Posts 44
Joined 2003-08-14 00:00
22-year member
UID 8600
Gender Male
Status Offline
I want to ask, what is the error code, 0 or 1, when entering a non-existent drive letter in cmd?
I modified it. The cmd is as follows:

@echo off
z:
IF Errorlevel 0 GOTO error
echo You don't have a z drive
goto exit
:error
echo You have a z drive
:exit
pause

Why is this still not working?
Floor 4 Posted 2006-09-10 06:20 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Error codes are rather complicated. Nowadays, users might have forgotten about them. Unfortunately, I haven't had much exposure to them and can't be of much help. I suggest you don't judge by the return code.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 5 Posted 2006-09-10 06:51 ·  中国 江苏 扬州 电信
中级用户
Credits 203
Posts 44
Joined 2003-08-14 00:00
22-year member
UID 8600
Gender Male
Status Offline
Then, how to solve it without using error codes?

Can the brother upstairs give a method?
Floor 6 Posted 2006-09-10 06:56 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
There are not many return codes found through online searches. You can try this yourself. You can use "if exist z:\nul" to make the judgment.
Floor 7 Posted 2006-09-10 06:58 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
In my XP system, I can use this statement to judge: if exist z: (echo You have drive Z) else (echo You don't have drive Z). There should be no problem in pure DOS. I don't know in what environment you want to use it.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 8 Posted 2006-09-10 07:13 ·  中国 江苏 扬州 电信
中级用户
Credits 203
Posts 44
Joined 2003-08-14 00:00
22-year member
UID 8600
Gender Male
Status Offline
Got it, I didn't expect that "if exist" can be used like this. I always thought it could only be used to judge whether a file exists.
Floor 9 Posted 2006-09-10 09:17 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
pushd C:>nul 2>nul && echo Yes ||echo No

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 10 Posted 2006-09-10 09:35 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Hehe, electronixtar's method is also good, but I don't know if the pushd command exists in pure DOS. After using this command, it's best not to forget to use popd in an appropriate place to return to the current directory.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Forum Jump: