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

Posted on 2007-10-03
Last Modified: 2008-03-05

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!
Question by:grjitdept
    LVL 30

    Accepted Solution

    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.)
    LVL 70

    Assisted Solution

    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
    LVL 1

    Author Comment

    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...
    LVL 70

    Expert Comment

    ah yes the /d switch - you could use that as well/instead - see the link i gave earlier.
    LVL 65

    Assisted Solution

    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....


    LVL 5

    Expert Comment

    Use robocopy to Mirror


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now