VBScript copy files from multiple partitions

April 1, 2010 at 10:09:22
Specs: Windows 7
How can I make the script below use multiple source locations and backup to the same, central destination?

Option Explicit
Dim fileCount
Const fromDir = "sourcedir"
Const toDir = "destdir"

fileCount = CopyXLS(CreateObject("Scripting.FilesystemObject"), fromDir, toDir)
On Error Resume Next
WScript.StdOut.WriteLine "Copied " & fileCount & " files."
WScript.Quit fileCount

'oFSO (object) - FileSystemObject to use
'sStart (string) = Source directory
'sEnd (string) - Destination directory
Function CopyXLS(oFSO, sStart, sEnd) 'As Integer
Dim d, f, i, ext
If Right(sEnd, 1) <> "\" Then _
sEnd = sEnd & "\"

If Not oFSO.FolderExists(sEnd) Then _
oFSO.CreateFolder sEnd

With oFSO.GetFolder(sStart)
For Each f In .Files
ext = oFSO.GetExtensionName(f)
'If Len(ext) And InStr(1, "xls", ext, 1) And _ ShouldCopy(oFSO, sEnd & f.Name, f.DateLastModified) Then
f.Copy sEnd
i = i + 1
'End If
Next 'f

For Each d In .SubFolders
i = i + CopyXLS(oFSO, d, sEnd & d.Name & "\")
Next 'd
End With
CopyXLS = i
End Function

'oFSO (object) - FileSystemObject to use
'sFile (string) - File (w/ path) to check
'dCutoff (date) - Date to compare to file's date.
Function ShouldCopy(oFSO, sFile, dCutoff) 'As Boolean
With oFSO
If Not .FileExists(sFile) Then
ShouldCopy = True
Exit Function
End If

ShouldCopy = .GetFile(sFile).DateLastModified < dCutoff
End With
End Function


See More: VBScript copy files from multiple partitions

Report •


#1
April 2, 2010 at 12:40:56
'-- i would slightly restructure the code:
dim froms(20)
froms(1)="f:\test"
froms(2)="g:\work"
froms(3)="c:\net"
froms(4)="d:\program files"
Const toDir = "destdir"
'fso only needs to be created once, so do it pre-loop
set fso=createobject("scripting.filesystemobject")
for i=1 to 4
filecount=copy Xls(fso,froms(i),toDir)
next i
On Error Resume Next
WScript.StdOut.WriteLine "Copied " & fileCount & " files."
WScript.Quit fileCount

Report •

#2
April 5, 2010 at 07:53:12
Thanks for your help. That worked :)

Report •

Related Solutions


Ask Question