![]() |
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-02 03:35 |
48,037 topics / 350,122 posts / today 2 new / 48,250 members |
| DOS批处理 & 脚本技术(批处理室) » Regarding the cmd to judge whether a disk partition exists |
| Printable Version 1,756 / 9 |
| Floor1 wjgyz740526 | Posted 2006-09-10 01:38 |
| 中级用户 Posts 44 Credits 203 | |
|
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. |
|
| Floor2 namejm | Posted 2006-09-10 02:13 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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. |
|
| Floor3 wjgyz740526 | Posted 2006-09-10 04:48 |
| 中级用户 Posts 44 Credits 203 | |
|
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? |
|
| Floor4 namejm | Posted 2006-09-10 06:20 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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.
|
|
| Floor5 wjgyz740526 | Posted 2006-09-10 06:51 |
| 中级用户 Posts 44 Credits 203 | |
|
Then, how to solve it without using error codes?
Can the brother upstairs give a method? |
|
| Floor6 vkill | Posted 2006-09-10 06:56 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
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.
|
|
| Floor7 namejm | Posted 2006-09-10 06:58 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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.
|
|
| Floor8 wjgyz740526 | Posted 2006-09-10 07:13 |
| 中级用户 Posts 44 Credits 203 | |
|
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.
|
|
| Floor9 electronixtar | Posted 2006-09-10 09:17 |
| 铂金会员 Posts 2,672 Credits 7,493 | |
|
pushd C:>nul 2>nul && echo Yes ||echo No
|
|
| Floor10 namejm | Posted 2006-09-10 09:35 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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.
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |