Access drive by name

H

Howard Brazee

Sometimes a removable drive on a networked computer has a different
drive letter than I had the previous time.

How can I write a cleaner batch file that accesses a drive by name? I
know the name, but the letter might be variable.

The following code works - but it is ugly code:

echo on
set mydrive=D:
dir %mydrive% 2>NUL | FIND /i "Brazee" >NUL
@If ErrorLevel 1 goto :tryE
@If not ErrorLevel 1 goto :foundIt
@If ErrorLevel 1 goto :Finale
pause You should not get here

:TryE
set mydrive=E:
dir %mydrive% 2>NUL | FIND /i "Brazee" >NUL
@If ErrorLevel 1 goto :tryF
@If not ErrorLevel 1 goto :foundIt
@If ErrorLevel 1 goto :Finale
pause You should not get here

:TryF
set mydrive=E:
dir %mydrive% 2>NUL | FIND /i "Brazee" >NUL
@If ErrorLevel 1 goto :tryG
@If not ErrorLevel 1 goto :foundIt
@If ErrorLevel 1 goto :Finale
pause You should not get here

:TryG
set mydrive=G:
dir %mydrive% 2>NUL | FIND /i "Brazee" >NUL
@If ErrorLevel 1 goto :tryH
@If not ErrorLevel 1 goto :foundIt
@If ErrorLevel 1 goto :Finale
pause You should not get here

:TryH
echo failure
goto :finale

:foundIt
pause foundit
:Finale
pause exit
 
N

Newbie Coder

This is not a newsgroup for writing batch files - sorry

XP has a bug with flash drives & assigning drive letters. Try assigning a
letter like Z to it & see it that holds it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top