Reputation: 441
I'm trying to make a code that detects if a drive letter exists.
For example, to check if C: drive exists my code is:
@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
But it doesn't work, it either goes to :yes if C: exists or shoes a blank screen if doesn't. What am I doing wrong, so that it won't go to :no?
Upvotes: 23
Views: 60491
Reputation: 999
All these answers bar the code with vol
use a backslash after the drive:colon
. Works without one:
if exist C: (
echo Yay C:
)
) else (
echo Nah
Edit: As with most things, there is a cost for cheaper, see Mofi's comments below. Alternatively, stretch the interpreter on the backslash by trying another 8190 of them after the C:
for the same result.
An alternative to backslash is a period or dot "." (or 8190 of them) after the drive:
if exist C:. (
echo Yay C:
)
) else (
echo Nah
Upvotes: 0
Reputation: 159
hmmpf!
C:\>net use
Status Local Remote Network
----------------------------------------------------------------------------
OK A: \\ENTDC01\Accounting_LawCorp Microsoft Windows Network
OK B: \\ENTDC01\Accounting_LLP Microsoft Windows Network
OK G: \\ENTDC01\GenShares Microsoft Windows Network
OK I: \\ENTDC01\ID Microsoft Windows Network
OK K: \\entdc01\KO_Holdings Microsoft Windows Network
OK L: \\ENTDC01\Library Microsoft Windows Network
OK P: \\ENTDC01\PDrives\rokimaw Microsoft Windows Network
OK Q: \\ENTDC01\soluno Microsoft Windows Network
OK S: \\entdc01\Synergy Scans Microsoft Windows Network
The command completed successfully.
c:\>if exist g:\ ( echo yes) else (echo no)
no
c:\>if exist p:\ ( echo yes) else (echo no)
yes
Upvotes: 0
Reputation: 70941
The main problem in your code is the if ... else
syntax. The full command needs to be read/parsed as a single block of code. It does not mean that it should be written in a single line, but if it is not, the lines must include information to the parser so it knows the command continues on the next line
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
)
Any of the previous codes will work as intended.
Anyway, the checking for the root folder of the drive will generate a popup for some kind of drives (in my case it was the multi card reader). To avoid it, use instead the vol
command and check for errorlevel
vol w: >nul 2>nul
if errorlevel 1 (
echo IT DOES NOT EXIST
) else (
echo IT EXISTS
)
Upvotes: 26
Reputation: 24470
@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
Upvotes: 7
Reputation: 10416
Verified to work under Win7. Try with an (existing and non-existing) drive letter of your choice:
@IF EXIST O:\ (GOTO cont1)
@ECHO not existing
@GOTO end
:cont1
@ECHO existing!
:end
Upvotes: 0