Copy Subfolders and Files within a certain date range

Hello,
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!!!
eshiramAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Joe Winograd, Fellow&MVEDeveloperCommented:
> 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:
http://ss64.com/nt/forfiles.html

Regards, Joe
eshiramAuthor Commented:
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.
NVITEnd-user supportCommented:
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

Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Joe Winograd, Fellow&MVEDeveloperCommented:
Hi NewVillageIT,

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

NewVillageIT first post
I thought that a post can't be deleted or edited if another post has been made after it. Regards, Joe
oBdACommented:
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.
Joe Winograd, Fellow&MVEDeveloperCommented:
In addition to getting details via robocopy /? as oBdA suggested, this website is excellent for learning about ROBOCOPY:
http://ss64.com/nt/robocopy.html

Regards, Joe
NVITEnd-user supportCommented:
@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.
Joe Winograd, Fellow&MVEDeveloperCommented:
I'll go with sunspots. :)
eshiramAuthor Commented:
@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!!
Joe Winograd, Fellow&MVEDeveloperCommented:
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
NVITEnd-user supportCommented:
eshiram,

Use my 2nd solution, the one linked by Joe.

The 1st solution (the FORFILES) can't copy a range.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NVITEnd-user supportCommented:
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.
eshiramAuthor Commented:
I used it (the second one) and it worked, thanks!
NVITEnd-user supportCommented:
Thanks for the update, eshiram. I'm glad you found a solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2008

From novice to tech pro — start learning today.