DavidOverton.com
This site is my way to share my views and general business and IT information with you about Microsoft, IT solutions for ISVs, technologists and businesses, large and small.  
How to copy or archive files by year using the command line and robocopy

I got this question via e-mail and decided I would post the answer here in a blog.  (Updated 16th Feb 2017 with a typo correction)

I want transfer all files created in Office Word 2003 by year (annual batchs - ex: 2005, 2006, etc.) to external HD. How can to do this?

I will appreciate your answer.

Edgar

To do this via the command line you can use Robocopy which is present in Windows Vista and Windows 7.  If you have Windows XP download the Server 2003 Resource Toolkit and install it.  Robocopy will be part of the install and found in the install directory.

The actual command would look like this and would need to be entered into a command prompt:

for /L %x in (2009,-1,2000) do robocopy source destination\%x *.doc? /maxage:%x0101 /minage:%x1231 /s

To break it down a bit..

  • for /L %x in (2009,-1,2000) do

This creates a counter from 2009 up in steps of -1 (or down in steps of 1) until 2000 is reached.  Each time the counter changes it is set into %x, so %x will become 2009, 2008 and so on.  It then executes the command after to "do".

  • robocopy source destination\%x *.doc? /minage:%x0101 /maxage:%x1231 /s

The robocopy command is very flexible.  Details of it can be found on the wiki page here

  1. The first two parameters are the sounce and destination locations.  so it might be c:\users\david\documents and F:\document_backup.  Note the \%x on the end.  This means it will put the files inside a folder on the destination location that matches the year you are archiving.
  2. *.doc? is a limiter to the files to be copied - if you want all files, simply remove it.  This will copy all Office 2003 and 2007 document files - if you only want Office 2003, use *.doc and don't include the question mark.
  3. The minage and maxage state how old the file is.  It can either be in days or a date in the form of yyyymmdd.  So with the %x set to 2009, the command will have /minage:20090101 /maxage:20091231 which equates to everything in 2009 and so on.
  4. The /s copies all subdirectories too
  5. You can optionally put the /MOV (to move the files) or /MOVE (to move all files and empty directories) command on the end.

 

I hope that helps

 

David


Posted Wed, May 6 2009 11:26 AM by David Overton

Comments

Steven Teiger wrote re: How to copy or archive files by year using the command line and robocopy
on Wed, Feb 15 2017 2:54 PM

Dave,

I tried using this and I think you've got your /minage and /maxage swapped around!

David Overton wrote re: How to copy or archive files by year using the command line and robocopy
on Thu, Feb 16 2017 9:17 AM

Steven, you are right - Ive just updated the blog post.  Appreciate the feedback!

Add a Comment

(required)
(optional)
(required)
Remember Me?

(c)David Overton 2006-23