How to write a vbscript to copy files recently modified but keep the folder sructure as well?


I am trying to write a vbscript that copies the contents of a folder (files and folder structure) to another folder, but ONLY where the files have been modified/created in the previous day.

I've found plenty of examples of how to copy files, folders or both, but I'm unsure how to go about selectively copying by modified date. For example, I'm fairly sure I could copy all files from a folder and subfolders into a single folder, but then I'd loose the original foldder struture... The key here is that I want to keep the folder structure of the files but only copy the recently modified ones...

Hope that makes sense!
Who is Participating?
Why not just use xcopy instead of vbscript?  It will maintain the folder structure, and there's a switch where, if the files already exist at the destination, to only copy files/folders that are newer than those already in place.  (I think it's the -d switch, but xcopy /? will give you a list of all available.)
Brian PiercePhotographerCommented:
Why write a script when you can do it with Xcopy

xcopy c:\source  d:\backup  /y /i /f /e
will copy all files and folder structure from c:\source to d:\backup - do this once in a while and at least once before...

xcopy c:\source  d:\backup /m /y /i /f /e

which will copy any files from c:\source to d:\backup if they have the been changed since they were last copied - run this regularly
grjitdeptAuthor Commented:
I've just read up on xcopy and it seems that would do the trick nicely! I'm still curious if there is a simple and effective way of doing a similar thing in vbscript so I'll the question open a bit longer...
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Brian PiercePhotographerCommented:
ah yes the /d switch - you could use that as well/instead - see the link i gave earlier.
Hi, in short, no, VBScript would not do this any easier, nor any faster.  XCopy would definately be the way to go, however, to incorporate that *into* a VBS script, just add these lines:

Set objShell = CreateObject("WScript.Shell")
strCommand = "XCopy /d:10-01-2007 /E /C /I /H /R /K /X /Y C:\SourceFolder C:\DestFolder"
objShell.Run strCommand, 1, True

You can change the 1 in the objShell.Run line to a 0 to hide the command prompt while it copies....


Use robocopy to Mirror

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.