Link to home
Create AccountLog in
Avatar of Ezra Shiram
Ezra ShiramFlag for United States of America

asked on

Copy Subfolders and Files within a certain date range

I have a drive with hundreds of folders with subfolders and files. I would like to copy all of that to another location but only the files that have been created within a certain date range (the year of 2014). Is this possible? If so, please help me with how to do this.

Thank You!!!
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

> created within a certain date range

Do you really mean the Created date? Or do you mean the Modified date? The /d parameter in the forfiles command posted above is for the Modified date, not the Created date. Here's a description of the forfiles command:

Regards, Joe
Avatar of Ezra Shiram


Hi ,
Thanks for the response!

1. Where does it say "echo @file" in both of your examples? I only see echo @path in the 1st line.
2. Does  your example run against all files before 2015? I need everything from 1/1/2014 to 12/31/2014. I don't need anything from 2013 and older. Is that possible?

I tested it with an example on my PC This is my example for the first line - forfiles /s /d -3/1/2015 /P "C:\Users\pcadmin\Downloads" /c "cmd /c echo @path" - which seemed to yield proper results.
And for the second line - I tried forfiles /s /d -3/1/2015 /c "cmd /c copy @path c:\move" but it did not work.
Be sure to test first.
Try this instead. Save this to a .BAT file of you choice.
Currently, it ECHOs the results to the screen. To run it for real, remove the ECHO fronting XCOPY.

@echo off
setlocal enabledelayedexpansion
set ChkYYYY=2014
set TgtDir=d:\target

for /r %%A in (*.*) do (
  set FD=%%~tA
  set YYYY=!FD:~6,4!
  if "!YYYY!" equ "%ChkYYYY%" (
    set SPath=%%~pA
    ECHO xcopy /i "%%~fA" "%TgtDir%!SPath!"

Open in new window

Hi NewVillageIT,

How did your first post disappear after mine had already posted? I mean this one:

User generated image
I thought that a post can't be deleted or edited if another post has been made after it. Regards, Joe
Avatar of oBdA

Use the command line tool robocopy.exe (part of the OS since Vista/Server 2008). I've used it to copy TB of data without issues.
The syntax is slightly different than copy or xcopy, it expects the source folder first, then the target folder, then the file mask (*.* if omitted), then other options.
Note that robocopy will by default skip files that already exist in the target folder in the same version (size and date), so it won't copy unnecessarily if you run the copy repeatedly.
Enter "robocopy /?" in a command prompt for details, it has lots of useful options.
You can start with something like this:
robocopy.exe "D:\Source\Folder" "E:\Target\Folder" *.* /s /r:0 /w:0 /np /tee /log:"C:\Temp\robocopy.log" /maxage:20140101 /minage:20141231 /TS /L

Open in new window

/s will process subfolders, /r:0 and /w:0 set the the retry count and wait time in seconds (usually only needed over unreliable networks), /np will disable the progress bar for larger files (because it uses control characters that make the log file hard to read), /tee sends the output to the console and a log file, and /log sets the log file.
/minage:YYYYMMDD is the minimum modified date, /maxage accordingly the maximum.
The last two options, /TS and /L, are for testing purposes. /TS includes the time stamp in the log file, and /L finally will tell robocopy to only log the files, but not actually copy anything .
Remove the /L to run it for real.
You can stop it anytime by pressing <Ctrl-C>.
If you wan to copy including NTFS permissions, add /copyall to the options.
In addition to getting details via robocopy /? as oBdA suggested, this website is excellent for learning about ROBOCOPY:

Regards, Joe
Maybe some kind of EE webpage rules going on. Or... sunspots, planetary alignment...

Just before picking Delete Comment, I didn't see your comment. In the past, doing that sometimes shows the error "Can't delete" (I don't recall exact phrase). After the delete, your post showed.
I'll go with sunspots. :)
@oBdA I tried using XCopy to test on a folder I don't need anymore, but for some reason nothing was copied. I did remove the L switch. It said it copied 3021 directories but I don't see anything there. And it said it skipped all of the files. I also played around with the date range but the stats gave the same exact results, which is weird.

@Joe Winograd I did see your post and tried that also. But that also did not work for me. In the first command I added a source and in the second I put in the destination.

forfiles /s /d -3/1/2015 /P "C:\Users\pcadmin\Downloads" /c "cmd /c echo @path"

Open in new window

forfiles /s /d -3/1/2015 /c "cmd /c copy @path c:\move"

Open in new window

What am I doing wrong?

Thank you both for your answers!!
Hi eshiram,
To be clear, those two "forfiles" statements are from NewVillageIT, not me. I was just trying to figure out how his post got deleted when I had posted after it (EE is not supposed to allow that). I had it in an old tab and so was able to take a screenshot of it before it disappeared. NewVillageIT and I agreed that the reason was sunspots. :)

Anyway, since it's his code, I'm sure he'll jump in, but note that he posted some new code after that. Regards, Joe
Avatar of NVIT
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
As for oBdA's code, I've tested it and it works. So, not sure what's happening at your end.
Here's the test I used. Note that I removed the /L so it runs for real:
set SrcDir=c:\users\me\documents
set TgtDir=c:\test\documents
robocopy.exe "%SrcDir%" "%TgtDir%" /s /r:0 /w:0 /np /tee /log:"%temp%\robocopy.log" /maxage:20140101 /minage:20141231 /TS

Open in new window

So both mine and oBdaA's code work here.
I used it (the second one) and it worked, thanks!
Thanks for the update, eshiram. I'm glad you found a solution.