?
Solved

vbs and arrays

Posted on 2010-01-11
9
Medium Priority
?
446 Views
Last Modified: 2012-06-22
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

0
Comment
Question by:crompnk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 11

Expert Comment

by:rajvja
ID: 26283150
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
 
LVL 56

Expert Comment

by:Bill Prew
ID: 26283343
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
 

Author Comment

by:crompnk
ID: 26284035
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 56

Expert Comment

by:Bill Prew
ID: 26284430
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
 
LVL 56

Expert Comment

by:Bill Prew
ID: 26284444
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
 

Author Comment

by:crompnk
ID: 26284490
Thanks.
ideally I will want to pass the list of files into the VB Script.
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 26284835
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
 
LVL 13

Accepted Solution

by:
game-master earned 2000 total points
ID: 26291188


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
 

Author Comment

by:crompnk
ID: 26292234
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Configuring Remote Assistance for use with SCCM
This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question