jparnell8839
jparnell8839

Reputation: 530

VBS doesn't complete loop when in sub, but does when not

Long story short, I have a VBS script I'm writing with nested For loops.

If I put the code in a sub, it doesn't complete the loop. If the code is out in the main, it does. Example:

Do
    For x = 0 to 9
        For y = 0 to 9
            For z = 0 to 9
                want = CStr(x) & "." & CStr(y) & "." & CStr(z)
                If want = "5.1.3"
                    Exit Do
                End If
            Next
        Next
    Next
Loop While 1 = 1

If this is out in main, it works. But this:

NestedLoop()

Sub NestedLoop()
    Do
        For x = 0 to 9
            For y = 0 to 9
                For z = 0 to 9
                    want = CStr(x) & "." & CStr(y) & "." & CStr(z)
                    If want = "5.1.3"
                        Exit Do
                    End If
                Next
            Next
        Next
    Loop While 1 = 1
End Sub

Will only loop to 2.0.5

Here's the full source code for each:

Nested Loop in Main

Nested Loop in Sub

Long story short, what it does is detects the OS arch (x86 or x64), goes out to VLC's download index (https://download.videolan.org/pub/videolan/vlc/last/win64/ or /win32/) and downloads to the working dir whatever version of the exe that resolves to HTTP status of 200. This also generates a log file at C:\Temp\vlc-installer.txt

So why would it not work in a sub? I know I could just have it in the main, but I'd prefer a sub.

FWIW, this is a "working interview" project. Me and another candidate for a position are tasked with coming up with an automated deployment solution.

Upvotes: 0

Views: 392

Answers (1)

Kul-Tigin
Kul-Tigin

Reputation: 16950

Both of your scripts have global On Error Resume Next statements, so I think you need to know more about how it works.
Please pay attention to the bold sentences.

From On Error Statement

[...]

Within any particular procedure, an error is not necessarily fatal as long as error-handling is enabled somewhere along the call stack. If local error-handling is not enabled in a procedure and an error occurs, control is passed back through the call stack until a procedure with error-handling enabled is found and the error is handled at that point. If no procedure in the call stack is found to have error-handling enabled, an error message is displayed at that point and execution stops or the host handles the error as appropriate.

On Error Resume Next causes execution to continue with the statement immediately following the statement that caused the run-time error, or with the statement immediately following the most recent call out of the procedure containing the On Error Resume Next statement. This allows execution to continue despite a run-time error. You can then build the error-handling routine inline within the procedure.

An On Error Resume Next statement becomes inactive when another procedure is called, so you should execute an On Error Resume Next statement in each called routine if you want inline error handling within that routine. When a procedure is exited, the error-handling capability reverts to whatever error-handling was in place before entering the exited procedure.

[...]

In your case, when an error occurs within your sub procedure called FindVLC, it immediately exits (jumps to End Sub) then continues to work on the global scope by ignoring errors.

So, you should execute an On Error Resume Next statement in your sub routine if you want to ignore (or handle) errors within that as the docs say.

Sub FindVLC()
    On Error Resume Next
    'for loops etc.
End Sub

Upvotes: 1

Related Questions