Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

vbs and arrays

Posted on 2010-01-11
9
Medium Priority
?
447 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
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 59

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 59

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 59

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 59

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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

Understanding the various editions available is vital when you decide to purchase Windows Server 2012. You need to have a basic understanding of the features and limitations in each edition in order to make a well-informed decision that best suits …
In this tutorial, we’re going to learn how to convert Youtube to mp3 for Free. We'll show you how easy it is to make an mp3 from your video clips so that you can enjoy them offline.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

581 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