如何从VBS中的函数内返回动态数组,以便以后在函数外部使用?

时间:2017-10-31 18:20:46

标签: arrays vbscript

我已经搜索了几个网站,并没有找到适合我需求的解决方案。我在VBScript中使用一个函数来创建两个数组,一个基于指定的日期范围,另一个是基于感兴趣位置的文件名的动态数组。然后我比较两个数组中的值并从主数组中删除重复项,并检查哪些值是工作日。

到目前为止,所有这些都有效。 我遇到的困难是能够通过" RangeArr()"函数之外的数组。请参阅下面的工作代码:

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

1 个答案:

答案 0 :(得分:1)

从函数返回内容的VBScript方法是将某些内容分配给函数的名称。演示:

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     

输出:

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
----------------

所以:

FindMissingReports = RangeArr

在函数的末尾,并且:

Dim a : a = FindMissingReports(TestPath)

在顶层。