0

I have two folders in C: drive.

I want to copy top 15 txt files from one folder to another folder. It is a daily repetitive task so I want to automate the process.

How can I copy those txt using batch script?

This is what I have but it does not work.

xcopy /s "C:\Documents" "C:\research"
Compo
  • 36,585
  • 5
  • 27
  • 39
A.S
  • 305
  • 1
  • 4
  • 20
  • 1
    Please tell us what defines the **top 15 `.txt`** files. – Compo Aug 24 '17 at 17:09
  • I mean I want to copy first 15 files. – A.S Aug 24 '17 at 17:11
  • 1
    Please tell us what defines the **first 15 `.txt`** files. – Compo Aug 24 '17 at 17:12
  • I have 100 txt file in a folder. I want to copy only top 15 files which are recently generated. – A.S Aug 24 '17 at 17:14
  • 1
    **…** that's a little better, so can you confirm that you want the **15 most recently modified** or perhaps the **15 most recently created**? – Compo Aug 24 '17 at 17:18
  • 2
    Possible duplicate of [How to batch-copy the 10 newest files to a directory in Windows?](https://stackoverflow.com/questions/44440967/how-to-batch-copy-the-10-newest-files-to-a-directory-in-windows) – Compo Aug 24 '17 at 17:29
  • [very related question](https://stackoverflow.com/questions/39830101/batch-file-that-keeps-the-7-latest-files-in-a-subfolder/39834842#39834842) – Stephan Aug 24 '17 at 18:21

2 Answers2

0
@echo off
setlocal EnableDelayedExpansion

set "i=0"
for /F "delims=" %%a in ('dir "C:\Documents" /O:-D') do if !i! lss 15 (
   copy "%%a" "C:\research"
   set /A i+=1
)

You may also add the /T switch in dir command to select the specific date used (creation, last access or last mod).

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thank you Aacini. In the code you have written above, if I have to copy only files that are created today then how do I do that? – A.S Aug 24 '17 at 20:36
  • 1
    My suggestion is that you accept an answer, _(if it provides the solution to your original question)_, then **start a new question**. – Compo Aug 25 '17 at 02:08
-1

I'd check this out:

Batch file to copy files from one folder to another folder

I think this is what you're looking for but I'm not sure it'll guarantee the first 15 files.

Since you don't have spaces the "" aren't required.

xcopy /s C:\Documents\*.txt C:\research\*.txt
Compo
  • 36,585
  • 5
  • 27
  • 39
Ragxion
  • 113
  • 10
  • Thank you. It works to copy all the files not just recently created 15 files. – A.S Aug 24 '17 at 18:36
  • No problem, if you wouldn't mind marking this as an answer that'd help build up my reputation. :) – Ragxion Aug 24 '17 at 18:41
  • I tried to mark this as answer but it does not record my mark since I am new to stackoverflow and I have less than 15 reputation. – A.S Aug 24 '17 at 18:46
  • #ReputationProblems – Ragxion Aug 24 '17 at 18:47
  • 1
    Please only mark an answer if it specifically answers the question posed. As this code is written, and as indicated in the second comment below the answer, it copies all text files. The only way it would copy the 15 newest would be if there were exactly 15 there. – Compo Aug 25 '17 at 10:09