vbs and arrays

Hi,
I have a vb script (see code) that will copy files form one folder to another depending on the file name and date.
Sometimes the files will have different names, how do I use an array in vbs to pass names of files and/or folders to the script.

For example if I have 3 files I want to copy all 3 together, i.e: Data1.csv, File1.csv and Work1.csv

In vbs can I pass a comma separated list i.e 'Data1,File1,Work1'
and only these files get copied.

Thank you
'DECLARE FOLLOWING VARIABLES
 
bPath = "C:\Temp\Test2\"                        'DESTINATION PATH
aBeginWith = "File1"                        'FILE BEGINS WITH
aDate = CDate("01-JAN-2010")                'FILES CREATED AFTER THIS DATE
 
'CREATE FILESYSTEM OBJECT
Set fso = CreateObject("Scripting.FileSystemObject")
 
'SET THE SOURCE PATH
Set aPath = fso.GetFolder("C:\Temp\Test")        'SOURCE PATH
 
'SEARCH THE FILES WITH GIVEN CRITERIA AND COPY THEM TO DESTINATION PATH
For Each file In aPath.Files
If RIGHT(file.Name, 4) = ".csv" and Left(file.Name, Len(aBeginWith)) = aBeginWith and file.DateCreated > aDate Then
file.Copy bPath & file.Name
End If
Next

Open in new window

crompnkData Management SpecialistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rajvjaCommented:
Hi

You can pas as a comma delimited string and split into array. So the resultant array will have filenames.
Loop through the array to do your file operation.

0
Bill PrewCommented:
You can certainly pass an array in a VB Script program, the following example:

Dim FilesToCopy
FilesToCopy = array("file1.txt", "file2.txt", "file3.txt")
For Each f In FilesToCopy
  WScript.Echo "Main: " & f
Next
TestSub FilesToCopy
Sub TestSub(FileArray())
  For Each f In FileArray
    WScript.Echo "Sub: " & f
  Next
End Sub

will produce this output:

Main: file1.txt
Main: file2.txt
Main: file3.txt
Sub: file1.txt
Sub: file2.txt
Sub: file3.txt

~bp
0
crompnkData Management SpecialistAuthor Commented:
Hi,
Thanks,
I'm struggling with the syntax on this one, can you see the code below, how would I also check the date?
'DECLARE FOLLOWING VARIABLES

Dim FilesToCopy
FilesToCopy = array("file1.csv", "file2.csv", "file3.csv")
 
bPath = "C:\Temp\Test2\"                        'DESTINATION PATH
aDate = CDate("01-JAN-2010")                'FILES CREATED AFTER THIS DATE
 
'CREATE FILESYSTEM OBJECT
Set fso = CreateObject("Scripting.FileSystemObject")
 
'SET THE SOURCE PATH
Set aPath = fso.GetFolder("C:\Temp\Test")        'SOURCE PATH
 
'SEARCH THE FILES WITH GIVEN CRITERIA AND COPY THEM TO DESTINATION PATH
For Each f In FilesToCopy
TestSub FilesToCopy
Sub TestSub(FileArray())
  For Each f In FileArray
    file.Copy bPath & file.Name
  Next
End Sub

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Bill PrewCommented:
I would go back to your original code and do something like this:

'DECLARE FOLLOWING VARIABLES

bPath = "C:\Temp\Test2\"                        'DESTINATION PATH
FilesToCopy = array("file1.csv", "file2.csv", "file3.csv")
aDate = CDate("01-JAN-2010")                'FILES CREATED AFTER THIS DATE
 
'CREATE FILESYSTEM OBJECT
Set fso = CreateObject("Scripting.FileSystemObject")
 
'SET THE SOURCE PATH
Set aPath = fso.GetFolder("C:\Temp\Test")        'SOURCE PATH
 
'SEARCH THE FILES WITH GIVEN CRITERIA AND COPY THEM TO DESTINATION PATH
For Each file In aPath.Files
  For Each f In FilesToCopy
    If LCase(file.Name) = LCase(f) and file.DateCreated > aDate Then
      file.Copy bPath & file.Name
    End If
  Next
Next

~bp
0
Bill PrewCommented:
Looking at your original question again, did you want to be able to pass these into the VB Script when you run it (by cscript I assume?), or is hardcoding the list of files to copy okay?

~bp
0
crompnkData Management SpecialistAuthor Commented:
Thanks.
ideally I will want to pass the list of files into the VB Script.
0
Bill PrewCommented:
Here's an approach to allow passing the files names array on the command line as a comma delimited string.  I added logic to allow a default if no command line files are specified, but that may not be required for your case.  To run it then you could do:

cscript myprog.vbs "copy1.txt,copy2.txt,copy3.txt"

Here's the updated script:

'DECLARE FOLLOWING VARIABLES
bPath = "C:\Temp\Test2\"                         'DESTINATION PATH
aDate = CDate("01-JAN-2010")                     'FILES CREATED AFTER THIS DATE

' LOAD ARRAY OF FILES TO COPY FROM COMMAND LINE IF SUPPLIED, ELSE USE DEFAULT
If Wscript.Arguments.Count = 0 Then
  FilesToCopy = array("file1.csv", "file2.csv", "file3.csv")
Else
  FilesToCopy = Split(Wscript.Arguments(0), ",")
End If

'CREATE FILESYSTEM OBJECT
Set fso = CreateObject("Scripting.FileSystemObject")

'GET THE SOURCE PATH FOLDER
Set aPath = fso.GetFolder("C:\Temp\Test")        'SOURCE PATH

'SEARCH THE FILES WITH GIVEN CRITERIA AND COPY THEM TO DESTINATION PATH
For Each file In aPath.Files
  For Each f In FilesToCopy
    If LCase(file.Name) = LCase(f) and file.DateCreated > aDate Then
      file.Copy bPath & file.Name
    End If
  Next
Next

~bp
0
game-masterCommented:


good afternoon!

i was trying to read the thread and i got confuse on what u really wanted to happen..

if you wanted to copy the file from the array to destination folder, try something like this...

Dim FilesToCopy As String
    Dim sCopy() As String
    Dim i As Integer
   
    FilesToCopy = "file1.csv,file2.csv,file3.csv"
    bPath = "C:\Temp\Test2\"                    'DESTINATION PATH
    aDate = CDate("01-JAN-2010")                'FILES CREATED AFTER THIS DATE
   
    'CREATE FILESYSTEM OBJECT
    Set FSO = CreateObject("Scripting.FileSystemObject")

    sCopy = Split(FilesToCopy, ",")
   
    For i = 0 To UBound(sCopy)
        If Left(sCopy(i), 3) = "csv" Then
            'then copy the file to destination folder
            'do ur copy code here
           
           
        End If
    Next



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
crompnkData Management SpecialistAuthor Commented:
Hi,
Thanks,
I tried the following and get an error: Expected end of statement


Dim FilesToCopy As String
    Dim sCopy() As String
    Dim i As Integer
   
    FilesToCopy = "file1.csv,file2.csv,file3.csv"
    bPath = "C:\Temp\Test2\"                    'DESTINATION PATH
    aDate = CDate("01-JAN-2010")                'FILES CREATED AFTER THIS DATE
   
    'CREATE FILESYSTEM OBJECT
    Set FSO = CreateObject("Scripting.FileSystemObject")

    sCopy = Split(FilesToCopy, ",")
   
    For i = 0 To UBound(sCopy)
        If file.DateCreated > aDate Then
            'then copy the file to destination folder
            file.Copy bPath ^& file.Name
           
           
        End If
    Next
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.