Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbscript to copy files using array

Status
Not open for further replies.

747576

Programmer
Jun 18, 2002
97
GB
Hi,
I want to copy files from an array to a destination folder, I also want to check the date as well.
I tried the following script and get an error: Expected end of statement

Code:
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
 
1) I don't see where in your code you set the file object
2) Replace this:
file.Copy bPath ^& file.Name
with this:
file.Copy bPath & file.Name

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi,Thanks

I tried the change and get the same error. I'm running the vbs from the command prompt.

The files to copy are stored in the array and passed through the loop to then be copied to the destination path 'bpath', where should I set the source path.

I tried the script below as well, but get the same error:

Code:
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")
    
    'SET THE SOURCE PATH
    Set aPath = fso.GetFolder("C:\Temp\Test")        'SOURCE PATH

    sCopy = Split(FilesToCopy, ",")
   For Each file In aPath.Files
    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
   Next
 
As This", "As That" are not vbscript. Take them all out.
 
Hi,
How should the variables be declared:

Should it be this:

Dim FilesToCopy
Dim sCopy()
Dim i Integer
 
Those are then fine except "dim i Integer": no need Integer there.
 
Ok, this gives me a type mismach error.

Code:
Dim FilesToCopy
    Dim sCopy()
    Dim i
   
    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")
    
    'SET THE SOURCE PATH
    Set aPath = fso.GetFolder("C:\Temp\Test")        'SOURCE PATH

    sCopy = Split(FilesToCopy, ",")
   For Each file In aPath.Files
    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
   Next
 
>Dim sCopy()
[tt]Dim sCopy[/tt]
Its content comes from split().
 
the "split" function defines the array variable to store the split. If it is already dimensioned as a string, it cannot be used to store data of a different type. sCopy does not need dimensioning.

Code:
strFiles = "file1.csv,file2.csv,file3.csv"
strDestPath = "C:\Temp\Test2\"
objDate = CDate("01-JAN-2010")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strDestPath)

arrFiles = Split(strFiles, ",")

For Each objFile In objFolder.Files
    For i = 0 To UBound(arrFilesCopy)
        If (objFile.DateCreated > objDate) Then
            objFile.Copy strDestPath & objFile.Name
        End If
    Next
Next

-Geates

ps. Everyone has there own style of naming variables. Most use an identifier in the variable name to describe its type. s = string, a = array, o = object, b = bool. I like to use 3 letters (str, arr, obj, etc.) Even if the code will never see another set of eyes, it is a good practice to describe your variables for easy readability and understanding.
 
What about this (typed, untested) ?
Code:
Set FSO = CreateObject("Scripting.FileSystemObject")
strSource = "C:\Temp\Test\"     'SOURCE PATH
strDest = "C:\Temp\Test2\"      'DESTINATION PATH
dtmDate = CDate("01-JAN-2010")  'FILES CREATED AFTER THIS DATE
For Each strName In Array("file1.csv","file2.csv","file3.csv")
  If FSO.FileExists(strSource & strName) Then
    Set objFile = FSO.GetFile(strSource & strName)
    If objFile.DateCreated > dtmDate Then
      objFile.Copy strDest & strName, True
    End If
  End If
Next

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top