|
wjgyz740526
中级用户
  
积分 203
发帖 44
注册 2003-8-14
状态 离线
|
『楼 主』:
关于判断一个磁盘分区是否存在的cmd
使用 LLM 解释/回答一下
我想写一个判断特定磁盘分区(比如z盘)是否存在的cmd,有的话就执行特定命令,否则显示一个错误提示。cmd如下:
@echo off
z:
IF Errorlevel 0 GOTO right
echo 你没有z盘
:right
echo 你有z盘
pause
哪儿错了呢?
恳请赐教
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.
|
|
2006-9-10 01:38 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
因为CMD是从上至下顺序执行的——如果中途没有goto之类的跳转语句的话,所以,当执行到echo 你没有Z盘 这一句的时候,因为没有跳转语句,所以程序会继续往下走,执行:right的内容。
解决办法是:在:right之上加一句pause && goto :eof。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-9-10 02:13 |
|
|
wjgyz740526
中级用户
  
积分 203
发帖 44
注册 2003-8-14
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
我想问一下,如果在cmd中进入一个不存在的盘符系统的错误码是0还是1?
我修改了一下。cmd如下:
@echo off
z:
IF Errorlevel 0 GOTO error
echo 你没有z盘
goto exit
:error
echo 你有z盘
:exit
pause
这样还是不行?为什么?
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?
|
|
2006-9-10 04:48 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
错误码这个东西比较复杂,现在的用户可能都已经淡忘了。很不幸,对它我接触得不多,帮不上什么忙。建议你不用返回码来判断。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-9-10 06:20 |
|
|
wjgyz740526
中级用户
  
积分 203
发帖 44
注册 2003-8-14
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
那么,不用错误码该怎么解决呢?
楼上的兄弟能给出个方法吗?
Then, how to solve it without using error codes?
Can the brother upstairs give a method?
|
|
2006-9-10 06:51 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
返回码网上搜索也没有多少,这个自己试,可以用
if exist z:\nul 来判断
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.
|
|
2006-9-10 06:56 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
在我的XP系统中,可以用这条语句来判断:if exist z: (echo 你有Z盘) else (echo 你没有Z盘),在纯DOS下应该没什么问题的。不知道你想用在什么环境中。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-9-10 06:58 |
|
|
wjgyz740526
中级用户
  
积分 203
发帖 44
注册 2003-8-14
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
受教了,没想到if exist 还能这样,我一直以为只能判断文件是否存在呢
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.
|
|
2006-9-10 07:13 |
|
|
electronixtar
铂金会员
      
积分 7493
发帖 2672
注册 2005-9-2
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
pushd C:>nul 2>nul && echo 有 ||echo 无
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'>" |
|
2006-9-10 09:17 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
呵呵,electronixtar的方法也不错,只是不知道在纯DOS下是否存在pushd命令。用该命令之后,最好不要忘记在适当的地方用popd来返回到当前的目录下。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-9-10 09:35 |
|