检查驱动器号是否批量存在,否则转到另一段代码

我试图做一个代码,检测是否存在一个驱动器号。

例如,要检查C:驱动器是否存在,我的代码是:

@echo off title If Exist Test :main CLS echo. echo press any key to see if drive C:\ exists echo. pause>nul IF EXIST C:\ GOTO yes ELSE GOTO no :yes cls echo yes pause>nul exit :no cls pause>nul exit 

但它不起作用,它要么:是的,如果C:存在或鞋子如果没有空白的屏幕。 我做错了什么,所以它不会去:不是吗?

 @echo off title If Exist Test :main CLS echo. echo press any key to see if drive C:\ exists echo. pause>nul ::NB: you need the brackets around the statement so that the file ::knows that the GOTO is the only statement to run if the statement ::evaluates to true, and the ELSE is separate to that. IF EXIST C:\ (GOTO yes) ELSE (GOTO no) ::I added this to help you see where the code just runs on to the ::next line instead of obeying your goto statements echo no man's land :yes ::cls echo yes pause>nul exit :no ::cls echo no pause>nul exit 

你的代码中的主要问题是if ... else语法。 完整的命令需要被读取/解析为一个单独的代码块。 这并不意味着它应该写在一行,但如果不是,行必须包括信息给解析器,所以它知道命令继续在下一行

 if exist c:\ ( echo exists ) else ( echo does not exist) ---- if exist c:\ ( echo exists ) else echo does not exist ---- if exist c:\ ( echo exists ) else echo does not exist ---- if exist c:\ ( echo exists ) else ( echo does not exist ) 

任何以前的代码将按预期工作。

无论如何,检查驱动器的根文件夹将生成一些类型的驱动器弹出(在我的情况下,它是多卡读卡器)。 为了避免它,请使用vol命令并检查错误级别

 vol w: >nul 2>nul if errorlevel 1 ( echo IT EXISTS ) else ( echo IT DOES NOT EXIST ) 

经验证可以在Win7下工作。 尝试使用您选择的(现有和不存在的)驱动器盘符:

 @IF EXIST O:\ (GOTO cont1) @ECHO not existing @GOTO end :cont1 @ECHO existing! :end