NDog1122
NDog1122

Reputation: 3

How can I return a dynamic array from within a function in VBS for later use outside the function?

I've searched through a few websites and have not had any success in finding a solution that fits my needs. I am using a function in VBScript to create two arrays, one based on a specified date range, and the other is a dynamic array based on the filenames at the location of interest. I then compare the values in the two arrays and remove the duplicates from the main array, and check to see which values are weekdays.

So far all of that works. The difficulty I am having is being able to pass the "RangeArr()" array outside of the function. See my working code below:

FindMissingReports(TestPath)

Function FindMissingReports(Path)

Dim FileName, File
Dim RangeArr()
intSize = 0
For i = 0 to 7
    ReDim Preserve RangeArr(intSize)
    RangeArr(intSize) = Year(Date - i) & "-" & Month(Date - i) & "-" & Day(Date - i)
    intSize = intSize +1
Next
'
Dim FileArr()
intSize = 0
'
Set oFSO = CreateObject("Scripting.FileSystemObject")
'Create the object used to display popup boxes
Set objShell = WScript.CreateObject("WScript.Shell")
'Loop through all of the files in the "Path" directory.
For Each File in oFSO.getfolder(Path).Files
    'If the file name contains "Defect Report"
    If instr(File.Name, "Defect Report") <> 0 Then
        Set objFile = oFSO.GetFile(File)
        'Define the filename as a variable
        FileName = File.Name
        'Get the report date from the first 10 characters of the filename.
        FileDate = Left(FileName, 10)
        ReDim Preserve FileArr(intSize)
        FileArr(intSize) = FileDate
        intSize = intSize +1
    End If
Next
'
For i = 0 to UBound(FileArr)
    For j = 0 to UBound(RangeArr)
        If UBound(RangeArr) > UBound(FileArr) and UBound(FileArr) <> -1 Then
            On Error Resume Next
            If FileArr(i) = RangeArr(j) Then
                removalIndexFile = i
                For x = removalIndexFile to UBound(FileArr) -1
                    FileArr(x) = FileArr(x+1)
                Next
                ReDim Preserve FileArr(UBound(FileArr)-1)
                removalIndexRange = j
                For x = removalIndexRange to UBound(RangeArr) -1
                    RangeArr(x) = RangeArr(x+1)
                Next
                ReDim Preserve RangeArr(UBound(RangeArr)-1)
            End If
        End If
    Next
Next
'
For i = 0 to UBound(RangeArr)
If IsWeekday(RangeArr(i)) Then
    MsgBox(RangeArr(i) & ".  It worked!  This is the only weekday report missing from the list.")
End If
Next
'
End Function

Function IsWeekday(theDate)
    IsWeekday = Weekday(theDate,vbMonday) <= 5
End Function

Upvotes: 0

Views: 505

Answers (1)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

The VBScript way to return something from a function is to assign that something to the function's name. Demo:

Option Explicit


' To return x from a function, assign x to the function's name
Function f(p)
  Select Case p 
    Case "Array()"
      f = Array("array via Array()")
    Case "FuncLikeSplit()"
      f = Split("func-returns-(dyn)-array")
    Case "DimReDimAssign"
      Dim tmp
      ReDim tmp(0)
      tmp(0) = "Dim-ReDim-Assign"
      f = tmp
    Case Else
      WScript.Echo "Error!"
  End Select
End Function

Dim a, p
' prove for each a: it's a dynamic array
For  Each p In Split("Array() FuncLikeSplit() DimReDimAssign")
     a = f(p)
     WScript.Echo p, TypeName(a), UBound(a), a(0)
     ReDim Preserve a(Ubound(a) + 1)
     a(UBound(a)) = "grownup"
     WScript.Echo UBound(a), a(UBound(a))
     WScript.Echo "----------------"
Next     

output:

cscript 47042147.vbs
Array() Variant() 0 array via Array()
1 grownup
----------------
FuncLikeSplit() Variant() 0 func-returns-(dyn)-array
1 grownup
----------------
DimReDimAssign Variant() 0 Dim-ReDim-Assign
1 grownup
----------------

So:

FindMissingReports = RangeArr

at the end of the function, and:

Dim a : a = FindMissingReports(TestPath)

at the top level.

Upvotes: 1

Related Questions