Question Locked

create batch file to select *.txt created today and copy to other folder

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

i'm trying to select the *.txt files created today in c:\users folder and then xcopy it to another folder in the system. the code i came up with is below but it doesnt run. can you help me fix the issue

forfiles /p c:\Users /m *.txt /d -0 /c "xcopy c:\Users c:\Users\kanishka /v /s /y"

7 replies

by Member 25348569

Never heard of 'forfiles'

However...

for /F %%i in ('dir /b c:\Users\*.txt') do copy c:\Users\%%i c:\Users\kanishka

will do what I think you want

by kanignu

This worked as a charm!! but i need to be able to select the files based on the created date. in other words i only need to select *.txt files created on that day

by Member 25348569

You can't - unfortunately - filter the DIR /B info because you don't have the date information.

Instead of "COPY", use Microsoft's ROBOCOPY and include the /MAXAGE:1 switch and that will limit the copy to files up to a day old.

You can also use XCOPY with the /D:DD-MM-YY (or it may be MM-DD-YY, I can't remember) and you get the date by the following bit of batch file stuff:

FOR /F "tokens=1,2,3* Delims=/ " %%I in ('DATE /T') DO set TODAY=%%I-%%J-%%K

Note that gets you whatever format DATE /T gets you! No idea which way round the dates should be so experiment.

You can then use /D:%TODAY% in the XCOPY command.

by oldbaritone

I'm just wondering if the actual requirement says "today" or if you are supposed to copy files that have been modified?

XCOPY has several options.

The /M option uses the Archive bit to determine what has been modified and what has not; the Archive bit is set automatically by the OS when a file is modified. XCOPY will clear the bit with the /M option, and copy only the files that have been modified.

The /D:m-d-y option copies only files changed on or after the specified date. This may be another way to accomplish your task.

http://www.computerhope.com/xcopyhlp.htm

by Member 25348569

is in automating the process and extracting today's date. Robocopy /Maxage:1 is probably the easiest.

by kanignu

I think i can use the Robocopy /Maxage:1 option Thanks for pointing me at the right direction

This discussion is closed to new replies.