如何使用数组将变量传递给VBScript

时间:2016-09-07 18:00:46

标签: vbscript

我正在尝试将文件夹位置作为变量传递给VBScript,该VBScript具有将该位置用作参数的数组。我不知道如何通过它,有人可以帮助我吗?

我正在尝试将以下位置作为变量" C:\ New"," C:\ New1"对于下面的代码,当我直接给出位置时,脚本工作正常,但是当我厌倦了将它作为变量传递时它不起作用。

以下代码:

Set oParameters = WScript.Arguments
folderlocation = oParameters(0)
Dim folderarray
Dim WshShell, oExec
Dim wow()
Set objShell = CreateObject("WScript.Shell")
Dim oAPI, oBag
Dim fso, folder, file
Dim searchFileName, renameFileTo, day
Dim i
folderarray = Array(folderlocation)
ii = 0
day = WeekDay(Now())
If day = 3 Then
    aa = UBound(folderarray)
    f = 0
    j = 0
    x = 0
    Y = 0
    For i = 0 To aa
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set folder = fso.GetFolder(folderarray(i))
        For Each file In folder.Files
            If InStr(file.Name, name) = 1 Then
            ii = 1
            strid = file.Name
            Set re = New RegExp
            re.Pattern = ".*myfile.*"
            If re.Test( strid ) Then
                'msgbox "File exist and the file name is """ & strid & """"
                x = x+1
            Else
                'msgbox "file not found"
            End If
            Set re = Nothing
          End If
        Next
        If x = 0 Then
            ReDim Preserve wow(f)
            wow(f) = folderarray(i)
            f = f+1
            j = j+1
        Else
            x = 0
        End If
    Next
End If

If J > 0 Then
    ReDim Preserve wow(f-1)
    value = Join(wow, ",")
    MsgBox "Files not found in the following location(s) :" & value
Else
    MsgBox "fine"
End If

1 个答案:

答案 0 :(得分:0)

要从参数列表中填充数组,您可以像这样调用脚本:

your.vbs "C:\New" "C:\New1"

并在your.vbs中填充数组,如下所示:

size = WScript.Arguments.Unnamed.Count - 1
ReDim folderarray(size)
For i = 0 To size
  folderarray(i) = WScript.Arguments.Unnamed.Item(i)
Next

如果由于某种原因你必须将文件夹列表作为单个参数传递,你可以像这样调用脚本:

your.vbs "C:\New,C:\New1"

并在your.vbs中填充数组,如下所示:

folderarray = Split(WScript.Arguments.Unnamed.Item(0), ",")