Detect USB drive path from batch file

Eidos Lego star wars (xbox)
October 1, 2009 at 17:22:46
Specs: Windows Vista, p 4 / 4 GB
Hello
this is my second topic here, as i'm learning batch file coding and i'm new learner and all by my own, no one helping me, so i found this site is very helpful for someone like me.
here is a code i made from 3 days to detect my USB and tell me which drive and path of it,
but i find it very long, and i was wondered am i using FOR command in bad long way?
can this code be smaller?
where i can find such information about batch files in details to learn?
here is the code:

@ECHO OFF
TITLE USB Searcher . . .
CLS

:START1

ECHO/
ECHO/ (S) Start Search For HaMaDa USB.
ECHO/ (Q) Quit
ECHO/
CHOICE /C:QS /N /M " :: Press (S) to Start Search or (Q) to exit:
IF ERRORLEVEL 2 GOTO START
GOTO EOF

:START
CLS

ECHO/
:: SET ALL VARIABLE TO NUL
SET USB1=
SET USB2=
SET USB3=
SET USB4=
SET USB5=
SET USB6=
SET USB7=
SET USB8=
SET USB9=
SET USB10=

ECHO/
ECHO/ Searching for HaMaDa USB Drives . . .
ECHO/

:: FOR EACH LETTER FROM THIS LIST MAKE THE ACTION OF DIR %%LETTER AND IF EXIST %%LETTER\xam\ --- SET A VARIABLE FOR USB FROM 1 TO 10
:: FOR EXAMPLE:
:: %USB1% WILL BE [F:\]
:: %USB2% WILL BE [G:\] ----- AND SO ON.

:: FOR EACH LETTER FROM THIS LIST MAKE THE ACTION OF DIR %%LETTER AND IF EXIST %%LETTER\xam\
:: AND IF THE LETTER NOT EQUAL TO THE PREVIOUS ONE --- SET ANOTHER USB FROM 2 TO 10 --------- AND SO ON TILL THE END.

:: IF THE VARIABLE %%LETTER = %% LETTER (MEANS THAT THE LOOP DIDN'T RETURN ANY POSSITIVE RESULT TO FIND THE FOLDER
:: SO GOTO WHATEVER FINISH-END-DONE

:USB1
for %%A in (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 (
dir %%A:\ 2>nul 1>&2&&if exist "%%A:\xam\" set "USB1=%%A:\"&&GOTO USB2S
)
IF %%A==%%A SET USB1=
GOTO ERROR

:USB2S
ECHO/ Found HaMaDa USB Drive . . . . . [%USB1%]
for %%B in (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 (
dir %%B:\ 2>nul 1>&2&&if exist "%%B:\xam\" IF %USB1% NEQ %%B:\ set "USB2=%%B:\"&&GOTO USB3S
)
IF %%B==%%B SET USB2=
GOTO DONE

:USB3S
ECHO/ Found HaMaDa USB Drive . . . . . [%USB2%]
for %%C in (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 (
dir %%C:\ 2>nul 1>&2&&if exist "%%C:\xam\" IF %USB1% NEQ %%C:\ IF %USB2% NEQ %%C:\ set "USB3=%%C:\"&&GOTO USB4S
)
IF %%C==%%C SET USB3=
GOTO DONE

:USB4S
ECHO/ Found HaMaDa USB Drive . . . . . [%USB3%]
for %%D in (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 (
dir %%D:\ 2>nul 1>&2&&if exist "%%D:\xam\" IF %USB1% NEQ %%D:\ IF %USB2% NEQ %%D:\ IF %USB3% NEQ %%D:\ set "USB4=%%D:\"&&GOTO USB5S
)
IF %%D==%%D SET USB4=
GOTO DONE

:USB5S
ECHO/ Found HaMaDa USB Drive . . . . . [%USB4%]
for %%E in (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 (
dir %%E:\ 2>nul 1>&2&&if exist "%%E:\xam\" IF %USB1% NEQ %%E:\ IF %USB2% NEQ %%E:\ IF %USB3% NEQ %%E:\ IF %USB4% NEQ %%E:\ set "USB5=%%E:\"&&GOTO USB6S
)
IF %%E==%%E SET USB5=
GOTO DONE

:USB6S
ECHO/ Found HaMaDa USB Drive . . . . . [%USB5%]
for %%F in (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 (
dir %%F:\ 2>nul 1>&2&&if exist "%%F:\xam\" IF %USB1% NEQ %%F:\ IF %USB2% NEQ %%F:\ IF %USB3% NEQ %%F:\ IF %USB4% NEQ %%F:\ IF %USB5% NEQ %%F:\ set "USB6=%%F:\"&&GOTO USB7S
)
IF %%F==%%F SET USB6=
GOTO DONE

:USB7S
ECHO/ Found HaMaDa USB Drive . . . . . [%USB6%]
for %%G in (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 (
dir %%G:\ 2>nul 1>&2&&if exist "%%G:\xam\" IF %USB1% NEQ %%G:\ IF %USB2% NEQ %%G:\ IF %USB3% NEQ %%G:\ IF %USB4% NEQ %%G:\ IF %USB5% NEQ %%G:\ IF %USB6% NEQ %%G:\ set "USB7=%%G:\"&&GOTO USB8S
)
IF %%G==%%G SET USB7=
GOTO DONE

:USB8S
ECHO/ Found HaMaDa USB Drive . . . . . [%USB7%]
for %%H in (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 (
dir %%H:\ 2>nul 1>&2&&if exist "%%H:\xam\" IF %USB1% NEQ %%H:\ IF %USB2% NEQ %%H:\ IF %USB3% NEQ %%H:\ IF %USB4% NEQ %%H:\ IF %USB5% NEQ %%H:\ IF %USB6% NEQ %%H:\ IF %USB7% NEQ %%H:\ set "USB8=%%H:\"&&GOTO USB9S
)
IF %%H==%%H SET USB8=
GOTO DONE

:USB9S
ECHO/ Found HaMaDa USB Drive . . . . . [%USB8%]
for %%I in (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 (
dir %%I:\ 2>nul 1>&2&&if exist "%%I\xam\" IF %USB1% NEQ %%I:\ IF %USB2% NEQ %%I:\ IF %USB3% NEQ %%I:\ IF %USB4% NEQ %%I:\ IF %USB5% NEQ %%I:\ IF %USB6% NEQ %%I:\ IF %USB7% NEQ %%I:\ IF %USB8% NEQ %%I:\ set "USB9=%%I:\"&&GOTO USB10S
)
IF %%I==%%I SET USB9=
GOTO DONE

:USB10S
ECHO/ Found HaMaDa USB Drive . . . . . [%USB9%]
for %%J in (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 (
dir %%J:\ 2>nul 1>&2&&if exist "%%J:\xam\" IF %USB1% NEQ %%J:\ IF %USB2% NEQ %%J:\ IF %USB3% NEQ %%J:\ IF %USB4% NEQ %%J:\ IF %USB5% NEQ %%J:\ IF %USB6% NEQ %%J:\ IF %USB7% NEQ %%J:\ IF %USB8% NEQ %%J:\ IF %USB9% NEQ %%J:\ set "USB10=%%J:\"&&GOT USB10F
)
IF %%J==%%J SET USB10=
GOTO DONE

:USB10F
ECHO/ Found HaMaDa USB Drive . . . . . [%USB10%]

:DONE
ECHO/
ECHO/ Search finished . . .
ECHO/

:NOMORE
ECHO/ No More HaMaDa USB Drives Found . . .
ECHO/
GOTO END

:ERROR
ECHO/ Search didn't find any HaMaDa USB Drive plugged in this computer!
ECHO/

:END
ECHO/ (S) Search again.
ECHO/ (Q) Quit
ECHO/
CHOICE /C:QS /N /M " :: Press (S) to search again or (Q) to exit:
IF ERRORLEVEL 2 GOTO START
GOTO EOFF

:EOFF
CLS

ECHO/
ECHO/ SEARCH FINISHED . . .
ECHO/
ECHO/ LIST OF ALL DRIVERs FOUND:
ECHO/
IF "%USB1%"=="" GOTO EOF
ECHO/ 1. HaMaDa USB IN . . . . . . [%USB1%]
IF "%USB2%"=="" GOTO EOF
ECHO/ 2. HaMaDa USB IN . . . . . . [%USB2%]
IF "%USB3%"=="" GOTO EOF
ECHO/ 3. HaMaDa USB IN . . . . . . [%USB3%]
IF "%USB4%"=="" GOTO EOF
ECHO/ 4. HaMaDa USB IN . . . . . . [%USB4%]
IF "%USB5%"=="" GOTO EOF
ECHO/ 5. HaMaDa USB IN . . . . . . [%USB5%]
IF "%USB6%"=="" GOTO EOF
ECHO/ 6. HaMaDa USB IN . . . . . . [%USB6%]
IF "%USB7%"=="" GOTO EOF
ECHO/ 7. HaMaDa USB IN . . . . . . [%USB7%]
IF "%USB8%"=="" GOTO EOF
ECHO/ 8. HaMaDa USB IN . . . . . . [%USB8%]
IF "%USB9%"=="" GOTO EOF
ECHO/ 9. HaMaDa USB IN . . . . . . [%USB9%]
IF "%USB10%"=="" GOTO EOF
ECHO/ 10. HaMaDa USB IN . . . . . . [%USB10%]
PAUSE

:EOF
choice /t 5 /c delay /d d >nul


See More: Detect USB drive path from batch file

Report •


#1
October 1, 2009 at 23:42:41
HaMaDa, one thing that jumps out (I'm pretty sure there're others too) is your repeated use of BCDEF... Why not set that as a variable and each time you need it just call the variable? Seems to me that'd be considerably shorter code.
HTH.
Ed in Texas.

Report •

#2
October 2, 2009 at 00:25:09
Thanks for reply
but honestly :-D if it was easy for me to do it by myself so i could do and post it here also.
:-) but what i made is the only thing that came into my mind.
actually i spent whole day trying to finish this code with lot of tries and fails and finally i got this working.
i know that it's tooooo long and for sure there are shorter way to get it work, that's what i'm asking about and how to do it.
about your idea it seems gr8 but forgive me, as i told i'm just a new learner and i didn't get all what you mean.
can u explain to me in examples, and i'll make it and re-post it again to ask your opinion.
thanks for your time in advance.

Report •

Related Solutions


Ask Question