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!

fso.copyfolder With Subfolders 2

Status
Not open for further replies.

SWarrior

MIS
Dec 19, 2003
111
US
Is there an EASY way to copy an entire folder's contents and EVERYTHING within it's Sub folder ONLY if the file is NEWER or UPdated ??

I can do this with one line using:
Code:
wshshell.run("cmd /c xcopy "D:\Path1\Path2\*.*" "Z:\Path1\" /d /s /e /q /h /r /o /x /y /c",1,true)
Would this still be the simplest way ? And YES, I do need all of those switches.... :)

I'd be more than happy to utilize an fso. option if I can do it in a couple lines... I'd like to keep things in VBS as much as possible these days.


-SWarrior
 
Hello SWarrior!

The following code will copy a folder, its subfolders, and its files from one location to another:
Code:
Dim objFileSys

Set objFileSys = CreateObject("Scripting.FileSystemObject")
objFileSys.GetFolder("C:\FromFolder\").Copy "C:\ToFolder\"
Set objFileSys = Nothing

Good luck!
 
Is there an easy way to do this so that it only copies NEW files or same name files that are NEWER? Just the same way that XCopy does this with all those command switches ??

I have the script working fine right now, but there is no need to re-copy files that are exactly the same as what is already there. Esentially, if we have a NEW laptop, or a rebuilt one, then we obviously want to take the time to copy all these file (several gig) but if they already exist, we only want to copy the new ones or updated ones ( a couple meg most days) See how important this is ??

Thanks again,
-SWarrior
 
Try modifying the copy statement to this:
Code:
objFileSys.GetFolder("C:\FromFolder\").Copy "C:\ToFolder\", False
If I understand the documentation from Microsoft correctly, this will not overwrite the files period, even if the file you are copying is newer. If this is what you want, great! If not, then I'm afraid you'll have to iterate through all folders and files, and do a date comparison against the folders and files you are overwitting. If this is the route you have to go, then more details on the file system object can be found at:
or even at one of the tektips partners:

Good luck!
 
>[tt]wshshell.run("cmd /c xcopy "D:\Path1\Path2\*.*" "Z:\Path1\" /d /s /e /q /h /r /o /x /y /c",1,true)[/tt]
[tt]wshshell.run[COLOR=red yellow] [/color]"cmd /c xcopy [COLOR=red yellow]"[/color]"D:\Path1\Path2\*.*"[COLOR=red yellow]"[/color] [COLOR=red yellow]"[/color]"Z:\Path1\"[COLOR=red yellow]"[/color] /d /s /e /q /h /r /o /x /y /c",1,true[COLOR=red yellow]
[/color][/tt]
 
tsuji,

Both ways for xcopy will actually work, and work correctly, however, I'd really like to be able to do this with .VBS without "Shelling" out, and to do it as efficiently as xcopy works. I've tried my script using the fso.copyfolder and it takes the same amount of time to process if the files are not there, as it does if they are there. I'm guessing that it's copying the files regardless if they already exist or not.

I have about 12 different locations that need to perform this "mass copy". So, I'm thinking that if I can do this as a Function or Sub, that might help... Probobly a Sub would be easier, because I need to be able to write to my 2 dimensional array durring this copy process to create my report that will get emailed after it's complete.

-SWarrior
 
SWarrior,

Granted xcopy works, I meant only it can't possibly work with the line as you presented without taking out the enclosing () etc.

- tsuji
 
SWarrior,

I created a script like this once.
Maybe this is what you need...

Code:
Option Explicit
Dim srcFolder, trgFolder

srcFolder = "C:\SourcePath"
trgFolder = "U:\DestinationPath"

CopyFilesAndFolders srcFolder, trgFolder
WScript.Quit

Sub CopyFilesAndFolders (ByVal strSource, ByVal strDestination)
    Dim ObjFSO, ObjFolder, ObjSubFolder, ObjFile, files
    Dim TargetPath
    Set ObjFSO = CreateObject("scripting.filesystemobject")
    'connecting to the folder where is going to be searched
    Set ObjFolder = ObjFSO.GetFolder(strSource)
    TargetPath = Replace (objFolder.path & "\", strSource, strDestination,1,-1,vbTextCompare)
    If Not ObjFSO.FolderExists (TargetPath) Then ObjFSO.CreateFolder (TargetPath)
    Err.clear
    On Error Resume Next
    'Check all files in a folder
    For Each objFile In ObjFolder.files
        If Err.Number <> 0 Then Exit For 'If no permission or no files in folder
        On Error goto 0
        If CheckToCopyFile (objFile.path, TargetPath & "\" & objFile.name) Then 
        	objFSO.copyfile objFile.path, TargetPath & "\" & objFile.name, True
        End If
    Next
    'Recurse through all of the subfolders
    On Error Resume Next
    Err.clear
    For Each objSubFolder In ObjFolder.subFolders
        If Err.Number <> 0 Then Exit For 'If no permission or no subfolder in folder
        On Error goto 0
        'For each found subfolder there will be searched for files
        CopyFilesAndFolders ObjSubFolder.Path & "\", TargetPath & ObjSubFolder.name & "\"
    Next
    Set ObjFile = Nothing
    Set ObjSubFolder = Nothing
    Set ObjFolder = Nothing
    Set ObjFSO = Nothing
End Sub

Function CheckToCopyFile (ByVal strSourceFilePath, ByVal strDestFilePath)
	Dim oFSO, oFile, SourceFileModTime, DestFileModTime
	CheckToCopyFile = True
    Set oFSO = CreateObject("scripting.filesystemobject")
    If Not oFSO.FileExists (strDestFilePath) Then Exit Function
    Set oFile = oFSO.GetFile (strSourceFilePath)
    SourceFileModTime = oFile.DateLastModified
    Set oFile = Nothing
    Set oFile = oFSO.GetFile (strDestFilePath)
    DestFileModTime = oFile.DateLastModified
    Set oFile = Nothing
	If SourceFileModTime =< DestFileModTime Then CheckToCopyFile = False
	Set oFSO = Nothing
End Function



Please tell me if I'm wrong I like to learn from my mistakes...
_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
K0b3,

Thank you very much !! The script seems to be working fine so far, and the added bonus is that I did not have to tweak it much at all to work with my 2 dimensional array !!!

-SWarrior
 
Note to all...

No matter how you slice it... DOS Commands are STILL far faster than VBScripts. My script takes 56 minutes & 4 seconds to Process this update when there are NO files to update. But using the XCOPY to perform the same functions, the same routine takes only 3 minutes & 49 seconds.

Ya gotta love your command prompt and it's DOS Utilities !!
-SWarrior
 
tsuji,

Thanks for the tip on the wshshell.run. It does, however, run properly the way that I had it with the commands inside of the parens like so:
Code:
wshshell.run("cmd /c xcopy c:\bla\*.* z:\bla\ /d /s /e /q /h /r /o /x /y /c",1,true)
-SWarrior
 
A note on performance...

I've noticed that if you create a windows script component file (.wsc), performance is greatly increased there as opposed to regular .vbs. Perhaps it has something to do with the .wsc file being put in the registry???

 
SWarrior,

But you keep putting parentheses surrounding the 3 parameters... Maybe it is not me to tell you after all to make you feel good. Let somebody else tells you then.

- tsuji
 
tsuji,

MY BAD !!! I misplaced the one parens. It should have read like this:
Code:
wshshell.run("cmd /c xcopy c:\bla\*.* z:\bla\ /d /s /e /q /h /r /o /x /y /c"),1,true
And also, for some strange reason, that one script that I was working on LOOKED and ACTED like it was working the way that you suggested durring testing, but durring production it failed, so I re-edited the script with the parens as I've alway's used before, and now it appears to be working as desired. Not sure why, and won't really know for sure for a couple days, as I'm heading off to a seminar for the next two days, and we're reverting back to the OLD DOS Batch files while I'm gone (Just in case)

-SWarrior
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top