1
   

Loops in Batch files

 
 
Reply Tue 9 Dec, 2003 10:17 am
Does anybody have a really clear concise description of how FOR loops work in batch files. Some examples would be nice as well. I've read lots of sites worth of info on them but I cannot seem to get them to work.

Here's what I am trying to do. I have written a batch file which detects the OS (95, 98, XP) and if a certain CD is inserted in a drive (drives D through Z) using a rudimentary IF loop with an incrementing variable. First it checks option one and if the wrong errorlevel returns a variable is incremented
"set var=var1"
then it check option two and uses the same incrementing variable to tell what the correct answer is. This method works but is prone to errors due to its length, also non-standard drives (ZIP, empty CD drives, etc) tend to create message boxes and stall the loop. For the 30-odd variables I'm checking I use 200+ lines of code.
I believe, based on what Ithink I understand about them, that FOR loops would allow me to shorten this significantly as well as provide a way of eliminating some of the errors from non-standard drives.
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Discussion • Score: 1 • Views: 739 • Replies: 2
No top replies

 
yeahman
 
  1  
Reply Tue 9 Dec, 2003 10:52 am
var = 0;
: label
var = var + 1;
if (condition for success == true)
blah blah blah...
else
goto label;

or...
var = 1;
while (condition for success == false)
var = var +1;
0 Replies
 
music2myear
 
  1  
Reply Wed 10 Dec, 2003 10:49 am
ja, that's about what I've got, I think, but when you're checking all those variables it get's kinda exhaustive:

PS: this loop searches based on the label of the CD we burn, and all the drives are necessary because we map large numbers of network drives in our systems and so sometimes the drive is way down there in the alphabet.


:PRECD1
rem This loop determines your CD drive...
set DRIVE=
set CDLOOP=%CDLOOP%i
if "%CDLOOP%"=="i" GOTO ROMD
if "%CDLOOP%"=="ii" GOTO ROME
if "%CDLOOP%"=="iii" GOTO ROMF
if "%CDLOOP%"=="iiii" GOTO ROMG
if "%CDLOOP%"=="iiiii" GOTO ROMH
if "%CDLOOP%"=="iiiiii" GOTO ROMI
if "%CDLOOP%"=="iiiiiii" GOTO ROMJ
if "%CDLOOP%"=="iiiiiiii" GOTO ROMK
if "%CDLOOP%"=="iiiiiiiii" GOTO ROML
if "%CDLOOP%"=="iiiiiiiiii" GOTO ROMM
if "%CDLOOP%"=="iiiiiiiiiii" GOTO ROMN
if "%CDLOOP%"=="iiiiiiiiiiii" GOTO ROMO
if "%CDLOOP%"=="iiiiiiiiiiiii" GOTO ROMP
if "%CDLOOP%"=="iiiiiiiiiiiiii" GOTO ROMQ
if "%CDLOOP%"=="iiiiiiiiiiiiiii" GOTO ROMR
if "%CDLOOP%"=="iiiiiiiiiiiiiiii" GOTO ROMS
if "%CDLOOP%"=="iiiiiiiiiiiiiiiii" GOTO ROMT
if "%CDLOOP%"=="iiiiiiiiiiiiiiiiii" GOTO ROMU
if "%CDLOOP%"=="iiiiiiiiiiiiiiiiiii" GOTO ROMV
if "%CDLOOP%"=="iiiiiiiiiiiiiiiiiiii" GOTO ROMW
if "%CDLOOP%"=="iiiiiiiiiiiiiiiiiiiii" GOTO ROMX
if "%CDLOOP%"=="iiiiiiiiiiiiiiiiiiiiii" GOTO ROMY
if "%CDLOOP%"=="iiiiiiiiiiiiiiiiiiiiiii" GOTO ROMZ
GOTO NOCD

:ROMD
echo Checking D drive...
set DRIVE=D
GOTO ROMLOOP

:ROME
echo Checking E drive...
set DRIVE=E
GOTO ROMLOOP

:ROMF
echo Checking F drive...
set DRIVE=F
GOTO ROMLOOP

:ROMG
echo Checking G drive...
set DRIVE=G
GOTO ROMLOOP

:ROMH
echo Checking H drive...
set DRIVE=F
GOTO ROMLOOP

:ROMI
echo Checking I drive...
set DRIVE=I
GOTO ROMLOOP

:ROMJ
echo Checking J drive...
set DRIVE=J
GOTO ROMLOOP

:ROMK
echo Checking K drive...
set DRIVE=K
GOTO ROMLOOP

:ROML
echo Checking L drive...
set DRIVE=L
GOTO ROMLOOP

:ROMM
echo Checking M drive...
set DRIVE=M
GOTO ROMLOOP

:ROMN
echo Checking N drive...
set DRIVE=N
GOTO ROMLOOP

:ROMO
echo Checking O drive...
set DRIVE=O
GOTO ROMLOOP

:ROMP
echo Checking P drive...
set DRIVE=P
GOTO ROMLOOP

:ROMQ
echo Checking Q drive...
set DRIVE=Q
GOTO ROMLOOP

:ROMR
echo Checking R drive...
set DRIVE=R
GOTO ROMLOOP

:ROMS
echo Checking S drive...
set DRIVE=S
GOTO ROMLOOP

:ROMT
echo Checking T drive...
set DRIVE=T
GOTO ROMLOOP

:ROMU
echo Checking U drive...
set DRIVE=U
GOTO ROMLOOP

:ROMV
echo Checking V drive...
set DRIVE=V
GOTO ROMLOOP

:ROMW
echo Checking W drive...
set DRIVE=W
GOTO ROMLOOP

:ROMX
echo Checking X drive...
set DRIVE=X
GOTO ROMLOOP

:ROMY
echo Checking Y drive...
set DRIVE=Y
GOTO ROMLOOP

:ROMZ
echo Checking Z drive...
set DRIVE=Z
GOTO ROMLOOP

:ROMLOOP
rem Be sure to change the search string to the appropriate CD label...
%DRIVE%:
cd\
dir|find "Volume in drive %DRIVE% is MI_">nul
if errorlevel=1 GOTO PRECD1
set ROM=%DRIVE%
if "%SECONDVAR%"=="ROM" GOTO PREOS2
GOTO PREOS1
0 Replies
 
 

Related Topics

Clone of Micosoft Office - Question by Advocate
Do You Turn Off Your Computer at Night? - Discussion by Phoenix32890
The "Death" of the Computer Mouse - Discussion by Phoenix32890
Windows 10... - Discussion by Region Philbis
Surface Pro 3: What do you think? - Question by neologist
Windows 8 tips thread - Discussion by Wilso
GOOGLE CHROME - Question by Setanta
.Net and Firefox... - Discussion by gungasnake
Hacking a computer and remote access - Discussion by trying2learn
 
  1. Forums
  2. » Loops in Batch files
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.04 seconds on 05/23/2024 at 12:00:13