Move files based on date into new folder whilst keeping the struct

Discussion in 'Scripting' started by Dave Pearce, Dec 17, 2009.

  1. Dave Pearce

    Dave Pearce Guest

    Hello wonder if you can help.

    I have a folder with lot's of files written in it from 2005-present day.
    What i need to script is move files from this location and sort them by years
    whilst keeping the existing structure. I have looked at the syntax of xcopy
    and robocopy but not sure whats the best way to move forward for exmaple

    d:\docs\word\file1.doc (created in 2006) would move to d:\2006\word\file1.doc

    d:\docs\excel\john\file2.xls (created in 2005) would move to
    d:\2005\excel\john\file2.xls

    And so on. I can make the script once i have an idea on the best way to take
    it forward.

    Many Thanks in advance.

    Regards
    Dave
     
    Dave Pearce, Dec 17, 2009
    #1
    1. Advertisements

  2. Seeing that you mention robocopy and xcopy, you probably want a batch file
    rather than a VB Script file. Here is one that will do the trick:
    @echo off
    set Source=d:\Docs
    set Target=d:
    set Year=2005
    set /a NYear=%Year% + 1
    set Switches= /s /mov /MinAge:%NYear%0101
    robocopy "%Source%" "%Target%\%Year%" *.* %Switches%

    Adjust the first three "set" commands to suit your environment. Note that
    neither the Source nor the Target must have a trailing backslash. Run the
    file for the oldest year first, then move up by one year at a time. I urge
    you to do a trial run with a disposable source directory until you're
    satisfied with the result.
     
    Pegasus [MVP], Dec 17, 2009
    #2
    1. Advertisements

  3. Dave Pearce

    Dave Pearce Guest

    This worked perfectly. Did the /COPY first to test.

    Many Thanks for your help.

    Kind Regards
    Dave
     
    Dave Pearce, Dec 19, 2009
    #3
  4. Thanks for the feedback.
     
    Pegasus [MVP], Dec 19, 2009
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.