Arrays 如何从VBS中的函数内部返回动态数组,以便以后在函数外部使用?
我搜索了一些网站,没有找到一个适合我需要的解决方案。我正在使用VBScript中的函数创建两个数组,一个基于指定的日期范围,另一个基于感兴趣位置的文件名创建动态数组。然后比较两个数组中的值,从主数组中删除重复的值,并检查哪些值是工作日 到目前为止,所有这些都有效我遇到的困难是能够将“RangeArr()”数组传递到函数之外。请参见下面我的工作代码:Arrays 如何从VBS中的函数内部返回动态数组,以便以后在函数外部使用?,arrays,vbscript,Arrays,Vbscript,我搜索了一些网站,没有找到一个适合我需要的解决方案。我正在使用VBScript中的函数创建两个数组,一个基于指定的日期范围,另一个基于感兴趣位置的文件名创建动态数组。然后比较两个数组中的值,从主数组中删除重复的值,并检查哪些值是工作日 到目前为止,所有这些都有效我遇到的困难是能够将“RangeArr()”数组传递到函数之外。请参见下面我的工作代码: FindMissingReports(TestPath) Function FindMissingReports(Path) Dim FileN
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
findmisingreports(TestPath)
函数findmisingreports(路径)
文件名,文件名
Dim RangeArr()
intSize=0
对于i=0到7
ReDim保留范围ARR(intSize)
RangeArr(intSize)=年(日期-i)&“-”月(日期-i)&“-”日(日期-i)
intSize=intSize+1
下一个
'
Dim FileArr()
intSize=0
'
Set of so=CreateObject(“Scripting.FileSystemObject”)
'创建用于显示弹出框的对象
设置objShell=WScript.CreateObject(“WScript.Shell”)
'循环遍历“Path”目录中的所有文件。
对于oFSO.getfolder(Path).Files中的每个文件
'如果文件名包含“缺陷报告”
如果instr(File.Name,“缺陷报告”)为0,则
设置objFile=oFSO.GetFile(文件)
'将文件名定义为变量
FileName=File.Name
'从文件名的前10个字符获取报告日期。
FileDate=Left(文件名,10)
ReDim保留文件arr(intSize)
FileArr(intSize)=FileDate
intSize=intSize+1
如果结束
下一个
'
对于i=0到UBound(FileArr)
对于j=0到UBound(范围arr)
如果UBound(RangeArr)>UBound(FileArr)和UBound(FileArr)-1,则
出错时继续下一步
如果FileArr(i)=RangeArr(j),则
removalIndexFile=i
对于x=removalIndexFile到UBound(FileArr)-1
FileArr(x)=FileArr(x+1)
下一个
重拨保留文件arr(UBound(FileArr)-1)
removalIndexRange=j
对于x=从removalIndexRange到UBound(RangeArr)-1
RangeArr(x)=RangeArr(x+1)
下一个
重拨保留范围(UBound(RangeArr)-1)
如果结束
如果结束
下一个
下一个
'
对于i=0到UBound(RangeArr)
如果是工作日(RangeArr(i)),那么
MsgBox(RangeArr(i)和“。成功了!这是列表中唯一缺少的工作日报告。”)
如果结束
下一个
'
端函数
函数为工作日(theDate)
IsWeekday=Weekday(日期,vbMonday)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
----------------
因此:
在函数末尾,以及:
Dim a : a = FindMissingReports(TestPath)
在顶层。发布有效的代码有什么意义?显示从函数返回动态数组的尝试中不起作用的内容。所有这些内容可能重复,即将返回值设置为函数名称。再也没有什么让我惊讶的了。你知道,我本可以在三小时前发布,但如果OP没有尝试返回阵列,我会想得更好。
Dim a : a = FindMissingReports(TestPath)