Abhishek B.
Abhishek B.

Reputation: 5202

How to copy all files created between two dates using Command prompt?

I have written code in window batch file (eq. getFiles.bat ) that get all files within select date range. eq.

xcopy /S /D:01-10-2011  *.* C:\todaysFiles

But I want get all files in between two dates including From date and To date.
file extension is .cmd or .bat

Upvotes: 8

Views: 43425

Answers (4)

Jimadine
Jimadine

Reputation: 1064

The title of this question is slightly misleading. Based on the questioner's xcopy example, I believe the questioner wants to know "How to copy all files created...", not how to get all files (which I equate with list).

The questioner also states "file extension is .cmd or .bat". This could mean that the questioner wants to copy only files with these extensions but I think it means the questioner would like a batch script solution.

To use the following solution, open a command prompt and chdir to the folder where the files to be copied are located. Then enter the commands listed below, changing the SET values as appropriate.

SET DESTINATION=C:\destination
SET DATE_FROM=01/01/2005
SET DATE_TO=01/01/2007
> 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% )"

Note: this will copy files in subfolders as well as files in the top-level folder.

The SET values could be hard-coded directly into the > nul forfiles... line, meaning only one line is required, but for clarity I've used variable substitution.

A caveat is that it is based on date modified (original question asked for date created)

Credit to aschipfl (https://stackoverflow.com/a/36585535/1754517) for providing the inspiration for my answer.

Upvotes: 2

RKO
RKO

Reputation: 161

Here's a batch for viewing files by creation date by year. Easily changed to cmd prompt by removing the extra percentage symbols.

for %%a in (2011 2012 2013 2014 2015 2016 2017) do (
for /f %%i in ('xxcopy i:\podcasts\*.* /LL /ZS /Q /FC /DA:%%a-01-01 /DB:%%a-12-31 ^| find /c /v ""') do echo %%a: %%i
)

Upvotes: 0

Maxus
Maxus

Reputation: 11

You can also use the forfiles command. It can search recursively in all subfolders /s for files created in the selected root folder /p <Path>, execute commands on the selected files /c "<Command>", apply masks to search /m <SearchMask>, between a date range /d [{+|-}][{<Date>|<Days>}]

more info here, and here

Upvotes: 0

Guido Domenici
Guido Domenici

Reputation: 5256

If you're on Vista/Win7/WinServer2008 you can use robocopy like so:

robocopy c:\source c:\destination *.* /MAXAGE:20101231 /MINAGE:20111001

On XP, I'm not sure if there are built-in solutions short of using Powershell and the like.

Upvotes: 17

Related Questions