zorro1224
zorro1224

Reputation: 73

batch search for windows drive letter, if exists set as variable and execute command, else try another letter

I need to create a batch file that looks for a drive letter and executes code, otherwise looks for another letter and executes same code. I want to set letter as variable so I don't have to repeat code, something like:

if exist E:\ 
var = E:\
else
if exist D:\
var = D:\

mv var\SQL\...\...
del var\...\...

Will this logic work? Or is it better to use

if exist E:\ goto cont1
else goto newletter

:cont1
code

:newletter
if exist D:\ go to cont2
else goto end

:cont2 
code

:end

Upvotes: 1

Views: 4118

Answers (4)

Compo
Compo

Reputation: 38622

This should only iterate your mounted drives in reverse order;

@ECHO OFF
FOR /F "DELIMS=\" %%A IN ('MOUNTVOL^|FIND ":\"') DO CALL SET _=%%A %%_%%
FOR %%A IN (%_%) DO (ECHO=MV %%A\SQL\...\...
    ECHO=DEL %%A\...\...)
TIMEOUT -1 1>NUL

[EDIT /]

IF EXIST E:\ (SET drive=E:) ELSE (IF EXIST D:\ (SET drive=D:) ELSE (ECHO="No E:\ or D:\ drive found"))

Upvotes: 0

zorro1224
zorro1224

Reputation: 73

I found a good solution to my scenario, since I am only looking for 2 drives, I find an if else statement to work nicely

if exist E:\ ( SET drive=E: )
else if exist D:\ ( set drive=D: )
else ( echo "No E:\ or D:\ drive found")

This way, I can just set all code after this to a variable %drive% and do not have to repeat any code Thanks for everyones input

Upvotes: 0

aschipfl
aschipfl

Reputation: 34919

To get all available drives, you could use the wmic command:

wmic LogicalDisk GET DeviceID

You can also filter for certain drive types, like for local disks, for example (consult the Microsoft article Win32_LogicalDisk class for all the possible DriveType filter options):

wmic LogicalDisk WHERE DriveType=3 GET DeviceID

Then wrap around two for /F loops to get the output of the wmic command line (to not filter, simply remove the WHERE DriveType^=3 portion):

for /F "skip=1" %%D in ('wmic LogicalDisk WHERE DriveType^=3 GET DeviceID') do (
    for /F %%C in ("%%D") do (
        echo Do stuff with drive %%D\...
    )
)

The outer for /F loop captures the output of the wmic command line. Since wmic produces Unicode output which for /F has problems with (it leaves some orphaned carriage-return characters in the captured output), another for /F loop is nested to get rid of these artefacts.


If you do not want to use the wmic command for some reason, you could alternatively loop through all letters of the alphabet using a for loop and check whether the drive exists, like this:

for %%D in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
    if exist "%%D:\" (
        echo Do stuff with drive %%D:\...
    )
)

Here, no filtering by drive type is possible, of course.

Upvotes: 4

0x90h
0x90h

Reputation: 746

The following worked on my Windows 10 system for detecting drives :

@echo off
if not exist c: goto check2
set temp=C
goto sharedcode

:check2
if not exist d: goto error
set temp=D
goto sharedcode

:sharedcode
echo %temp%:\ exists!
goto end

:error
echo nothing exists!

:end

Upvotes: 0

Related Questions