Page 1 of 1
[Solved] Loop Through?
Posted: 06 Apr 2009 14:39
by Eagle710
I have a script that looks for a file inside of a USB Device. However the drive letter is not always the same. So I have code that changes to the drive letter checks if a file exists and if so will goto a Runit
Code: Select all
e:
if exist "Hello.exe" goto Runit
f:
if exist "Hello.exe" goto Runit
g:
if exist "Hello.exe" goto Runit
h:
if exist "Hello.exe" goto Runit
i:
if exist "Hello.exe" goto Runit
:Runit
Hello.exe
How would I make this a loop through a specific range/ the alphabet?
Posted: 06 Apr 2009 15:07
by avery_larry
Quick and easy way:
Code: Select all
for /f %%a in (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 "%%a:\hello.exe" (
set drive=%%a:
goto Runit
)
)
cls
echo.If you get here, hello.exe was not found on any drives.
echo.
pause
goto :eof
:runit
cls
echo.Use %drive% as the USB drive like this:
echo %drive%\hello.exe
echo.
pause
goto :eof
Posted: 14 Apr 2009 14:22
by Eagle710
I got it work. My next question is how can I check for both hello.exe and bonjour.exe . I would like to run which ever exe is noticied. I was think if possible an else If exist....with a second runit. Let me know if I am crazy.
Posted: 15 Apr 2009 10:07
by avery_larry
Code: Select all
for /f %%a in (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 "%%a:\hello.exe" (
set drive=%%a:
goto Runit
)
if exist "%%a:\bonjour.exe" (
set drive=%%a:
goto Runit
)
)
cls
echo.If you get here, hello.exe was not found on any drives.
echo.
pause
goto :eof
:runit
cls
echo.Use %drive% as the USB drive like this:
echo %drive%\hello.exe
echo.
pause
goto :eof
No need for for an ELSE because of the goto command -- it'll never reach the 2nd IF statement unless hello.exe doesn't exist.
Posted: 15 Apr 2009 14:35
by Eagle710
If it doesn't find hello.exe I want it to check t=for the drive that has bonour.exe but instead of running hello... I want it ro run bonjour.exe in that case.
Posted: 16 Apr 2009 14:18
by avery_larry
Do you mean only if all drives fail to find hello.exe then you want to search for bonjour.exe?
Or just whichever one you find you want to run either hello.exe or bounjour.exe?
Posted: 16 Apr 2009 14:49
by Eagle710
Which ever one it finds run it... so if drive a... has hello.exe run. However if Hello.exe doesn't exist but bonjour does run bonjour.
Posted: 17 Apr 2009 12:32
by avery_larry
Code: Select all
for /f %%a in (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 "%%a:\hello.exe" (
set drive=%%a:
set prog=hello.exe
goto Runit
)
if exist "%%a:\bonjour.exe" (
set drive=%%a:
set prog=bonjour.exe
goto Runit
)
)
cls
echo.If you get here, neither hello.exe nor bonjour.exe was found
echo.on any drives.
echo.
pause
goto :eof
:runit
cls
echo.Use %drive% as the USB drive and %prog% as the
echo.program like this:
echo %drive%\%prog%
echo.
pause
goto :eof
Posted: 22 Apr 2009 12:33
by Eagle710
I got it to work. Awesome code, I had to remove the /f from the for loop for it to work.
Thanks for your help.