Copy the last 7 written *.txt files in a directory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Copy the last 7 written *.txt files in a directory

#1 Post by MKANET » 02 Apr 2012 16:29

I tried using:

robocopy "%newremote%" "%home%\Today" BK*.txt /maxage:7

However, it looks like it just copies only the last seven days. I need a way to copy the last seven day's that were written in the directory. So, if the oldest file is from the end of January, it would copy everything from the last week of January.

The simpler the code, the better. Thanks so much in advance, you guys are great!!!

MKANET

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy the last 7 written *.txt files in a directory

#2 Post by foxidrive » 02 Apr 2012 18:23

How many files are written per day?

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Copy the last 7 written *.txt files in a directory

#3 Post by MKANET » 02 Apr 2012 18:36

It varies, anywhere from none to 4-5.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy the last 7 written *.txt files in a directory

#4 Post by foxidrive » 02 Apr 2012 18:39

Can you arrange so the files are written into folder with a date in the name? Like YYYYMMDD ?

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Copy the last 7 written *.txt files in a directory

#5 Post by MKANET » 02 Apr 2012 18:59

Only whats available in a dir command on Vista/Win7 command-line.

Edit: I know it can be done with the combination of a dir command piped to a for /f command; just not sure how without giving myself a headache :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy the last 7 written *.txt files in a directory

#6 Post by foxidrive » 02 Apr 2012 19:05

Can you arrange so the files are written into folder with a date in the name? Like YYYYMMDD ?


What process is creating the files?

It would take a lot of comparing to sort 7 days, when the days aren't consecutive and the file count is variable. Changing the way the files are created could be a better start and then a simple batch file can help.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Copy the last 7 written *.txt files in a directory

#7 Post by MKANET » 02 Apr 2012 19:26

If it were that easy, I wouldn't be posting here. :) I don't mind if the batch file takes a a little longer to process the files for comparison. Typical file name is: BK120320-002.txt (created by backup software). Unfortunately, I can't change the configuration of all our backup servers enterprise-wide.

PS: The filename formats are a bit different from machine to machine; so, I can't count on using BK- month=03, date=20, year=2012. That's why I didn't consider that option in the first place.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy the last 7 written *.txt files in a directory

#8 Post by foxidrive » 02 Apr 2012 19:45

What about keeping a certain number of the most recent files? say 50?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy the last 7 written *.txt files in a directory

#9 Post by foxidrive » 02 Apr 2012 20:06

Like this: to keep the most recent 7 txt files.

Make sure you test it on sample files.


Code: Select all

@echo off
for /f "skip=7 delims=" %%a in ('dir *.txt /a:-d /o:-d /b') do del "%%a"
pause

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Copy the last 7 written *.txt files in a directory

#10 Post by MKANET » 02 Apr 2012 20:35

Thanks, but I'm trying to do this without deleting the source files. Copying the entire directory contents to another location just to delete most of them doesnt sound very practical. I have to toy with a script right now, but can't get the counter to work. It's supposed to pipe the names of the 7 most recent filenames in a comma delimited text file which can be easily read in a subsequent for /f statement. Any ideas how to get the counter to work?

Code: Select all


set /a t=0

for /f " delims=" %%a in (
'dir "*.*"/b /a-d /o-d'
) do (
    Set /a T = T + 1
    echo %%a, >> recentfiles.txt
    if T==8 goto EOF
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy the last 7 written *.txt files in a directory

#11 Post by foxidrive » 02 Apr 2012 20:42

My mistake - you want to copy them... this is untested:


Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (
'dir *.txt /a:-d /o:-d /b'
) do (
copy "%%a" "c:\target folder\"
set /a num=num+1
if !num! EQU 7 goto :done
)
:done
pause

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Copy the last 7 written *.txt files in a directory

#12 Post by MKANET » 02 Apr 2012 20:50

oops... edit... trying now..

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Copy the last 7 written *.txt files in a directory

#13 Post by MKANET » 02 Apr 2012 20:56

Worked like a charm... setlocal enabledelayedexpansion was missing for the counter to work.

I appreciate all your time and effort!

Post Reply