I want to copy files that have date modified attribute values within a specific date range from one folder to another. I'm able to perform the copy using the below script but it's only copying the root level files within the parent directory of the source location
I need to copy the entire folder structure for files meeting the date range requirement.
SET DESTINATION=c:\temp\new
SET DATE_FROM=01/01/2019
SET DATE_TO=01/10/2019
> nul forfiles /S /D +%DATE_FROM% /C "cmd /C if @isdir==FALSE 2> nul forfiles /M @file /D -%DATE_TO% && > con ( echo @path && copy /V @path %DESTINATION% )"
pause
This batch script copies all the files within the specified date range to the root of the C:\temp\new
folder but it doesn't copy the source folder structure with it. I need the destination to also contain the matching source folder structure for copied files within the defined date range.
Best Answer
Copy Files and Folder Structure Recursively between a Date Range
You can use PowerShell and Get-ChildItem to get the
LastWriteTime
attribute values of files recursively in a starting source directory.For the source files that have the
LastWriteTime
attribute value within the date range, you can replace the source folder value with the destination folder value, and then use Copy-Item with the-Recurse -Force
parameters to create the matching structure and copy over the files.Note: I wrote this in both batch script logic and PowerShell script logic below for you.
Batch
PowerShell
Further Resources
ForEach-Object
Replace()