Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exit 2 (two) for loops after a condition is met.

Status
Not open for further replies.

lhailey

Programmer
Sep 24, 2010
4
US
Hi, I have a working script where I am trying to exit 2 (two) for loops after a condition is met. Can you help? I included the snippet that is in question and the entire script which is below.


Thank-you


Here is the snippet:

If n = 3 Then
print "Terminated after" & n & " loops"
'I want to exit both loops here.
End If
Next
Next
Loop



Here is the complete script:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' File Name: Read Last 6 Lines of Log file
'' Purpose: Read Last 6 Lines of Log file
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim arrFileLines(), s1, logfile
logfile = DataTable("Visible", dtLocalSheet)

'Loop
Do Until TextFound1
ForReading = 1 'Reads contents of entire file and then stores into an array
i = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\nelson\Desktop\cli.log", 1)
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
'print i 'prints # of lines in file
Loop
objFile.Close 'Closes file

For l = Ubound(arrFileLines) -6 to Ubound(arrFileLines) 'UBound function returns upper limit for # of elements in array
For n = 1 to 4 'loop break out start
If arrFileLines(l) = logfile Then
TextFound1 = True
print "!!!!!!!!!!!!!!!!!!!!! The text " & "'" & logfile & "'" & " was found !!!!!!!!!!!!!!!!!!!!!."
print vbcr
Reporter.ReportEvent 0, "Command Results:", logfile & " command ran successfully."
Exit For
' ElseIf n = 4 Then
' print n & "Exit Test after 4 loops"
' ExitTest
Else
print l & " The text " & "'" & logfile & "' " & " was not found."
print "Text " & "'" & arrFileLines(l) & "'" & " was found instead. "
print vbcr
End If
wait 2
print "Iteration " & n
If n = 3 Then
print "Terminated after" & n & " loops"
'go to another action
'what i really want to do is exit both for loops
ExitTest
End If
Next
Next 'end loop break out
Loop


 
The "Exit For" command will exit a for loop. Try sticking 2 of these in your code to break out of both loops.
 
i dont get the purpose of the loop:

For n = 1 to 4
'do some test
wait 2
Next

i mean, the thing you are checking 'arrFileLines(l) = logfile ' is not changing during the period of the loop, why loop?

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top