微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

例如,要检查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:存在或鞋子如果没有空白的屏幕。 我做错了什么,所以它不会去:不是吗?

有没有办法只redirectstderr标准输出(不结合这两个),所以它可以pipe道到其他程序?

pipe道输出时如何在batch file中显示消息?

获取启动分区驱动器名称

批量查找文件扩展名

如何使用命令输出作为另一个命令的参数?

批量文件更改

使用batch file删除具有条件的特定文件

将Stringreplace为多个文件

如何在Windows中随时捕获命令提示符的显示输出

如何用.bat脚本closures窗口?

@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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐