Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if drive letter exists in batch, or else goto another piece of code

Tags:

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?

like image 437
Twml Avatar asked Jun 05 '14 12:06

Twml


People also ask

How do you check if a drive is mapped in CMD?

On the command terminal, please then type the following: “net use”. 4 . Once this is entered, it will show you a full list of all the network drives mapped.

How do you check if a file exists in a folder in batch script?

You can check whether certain files are present using an 'IF EXIST' command. You can use this to execute commands via a batch file. If any spaces are present in the path name, you must place the entire path+filename+extension between straight double quotes.

What is %% A in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

Does batch have else if?

However, you can't use else if in batch scripting. Instead, simply add a series of if statements: if %x%==5 if %y%==5 (echo "Both x and y equal 5.") if %x%==10 if %y%==10 (echo "Both x and y equal 10.") else (echo "Invalid input.")


2 Answers

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 ) 
like image 188
MC ND Avatar answered Sep 28 '22 04:09

MC ND


@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 
like image 37
JohnLBevan Avatar answered Sep 28 '22 04:09

JohnLBevan