0

I am trying to pass folder location as variable to a VBScript which has array to consume the location as a parameter. I don't know how to pass it, could some one please help me?

I am trying to pass following location as a variable "C:\New","C:\New1" to the below code, the script is working fine when I directly give the location, but when I tired to pass it as variable it is not working.

Code given below:

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
7
  • Explain in more detail how it isn't working and what you are expecting. Its not working doesn't give a whole lot to go on. - I am guessing that you need to do a split on the comma to create the array, folderarray = Split(oParameters(0), ",") Commented Sep 7, 2016 at 18:11
  • Script does not take the variable passed from outside Example: tesxt.vbs "C:\New","C:\New1" is not working. Commented Sep 7, 2016 at 18:13
  • Let me make it simple, dim a Set oParameters = WScript.Arguments value= oParameters(0) 'a=Array(5,10,15,20) a=Array(value) msgbox a(3) I tried to execute above script with variable parameter "test.vbs 5,10,15,20 it gave me an error "Subscript out of range" Commented Sep 7, 2016 at 19:09
  • 1
    use the split function as I already suggested and this will remedy the problem. You seem to be insistent on using the Array function. : folderarray = Split(folderlocation, ",") Commented Sep 7, 2016 at 19:33
  • My intention is to check for a file on 2 different locations (this may vary depending up on a server) Hence I planned to grab the location in to an array and check it one by one. In order to reuse the script for different machine I am trying to pass the folder location as variable. I am not sure how split will help me here! Commented Sep 7, 2016 at 19:37

1 Answer 1

1

To fill an array from a list of arguments you'd call the script like this:

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

and fill the array in your.vbs like this:

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

If for some reason you must pass the folder list as a single argument you'd call the script like this:

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

and populate the array in your.vbs like this:

folderarray = Split(WScript.Arguments.Unnamed.Item(0), ",")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.