Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 959
Abhishek B. Avatar asked Jan 24 '11 06:01

Abhishek B.


People also ask

Can I copy my files from command prompt?

Use the Copy Command to Transfer Specific Files Right-click the Start button and choose "Command Prompt (Admin)" to open CMD. To copy files, use the copy command from the command line. copy c:\myfile.

What is %% in a batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

Which command is used to copy multiple files?

Use the cp command to create a copy of the contents of the file or directory specified by the SourceFile or SourceDirectory parameters into the file or directory specified by the TargetFile or TargetDirectory parameters.


2 Answers

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.

like image 139
Guido Domenici Avatar answered Sep 22 '22 01:09

Guido Domenici


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.

like image 28
Jimadine Avatar answered Sep 19 '22 01:09

Jimadine