TBG*_*500 15 windows cmd batch-file
我正在尝试创建一个代码来检测驱动器号是否存在.
例如,要检查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
Run Code Online (Sandbox Code Playgroud)
但它不起作用,它要么:如果C:存在则是,或者如果不存在则将其换成空白屏幕.我做错了什么,所以它不会去:不是吗?
MC *_* ND 13
代码中的主要问题是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
)
Run Code Online (Sandbox Code Playgroud)
任何以前的代码都可以按预期工作.
无论如何,检查驱动器的根文件夹将为某种驱动器生成弹出窗口(在我的情况下,它是多卡读卡器).要避免它,请使用vol命令并检查errorlevel
vol w: >nul 2>nul
if errorlevel 1 (
echo IT DOES NOT EXIST
) else (
echo IT EXISTS
)
Run Code Online (Sandbox Code Playgroud)
@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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34680 次 |
| 最近记录: |