[Solved] Loop Through?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Eagle710
Posts: 19
Joined: 29 Mar 2009 17:27

[Solved] Loop Through?

#1 Post by Eagle710 » 06 Apr 2009 14:39

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?
Last edited by Eagle710 on 22 Apr 2009 13:26, edited 1 time in total.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 06 Apr 2009 15:07

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

Eagle710
Posts: 19
Joined: 29 Mar 2009 17:27

#3 Post by Eagle710 » 14 Apr 2009 14:22

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.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#4 Post by avery_larry » 15 Apr 2009 10:07

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.

Eagle710
Posts: 19
Joined: 29 Mar 2009 17:27

#5 Post by Eagle710 » 15 Apr 2009 14:35

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.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#6 Post by avery_larry » 16 Apr 2009 14:18

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?

Eagle710
Posts: 19
Joined: 29 Mar 2009 17:27

#7 Post by Eagle710 » 16 Apr 2009 14:49

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.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#8 Post by avery_larry » 17 Apr 2009 12:32

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

Eagle710
Posts: 19
Joined: 29 Mar 2009 17:27

#9 Post by Eagle710 » 22 Apr 2009 12:33

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.

Post Reply