Copy the last 7 written *.txt files in a directory
Moderator: DosItHelp
Copy the last 7 written *.txt files in a directory
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
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
Re: Copy the last 7 written *.txt files in a directory
How many files are written per day?
Re: Copy the last 7 written *.txt files in a directory
It varies, anywhere from none to 4-5.
Re: Copy the last 7 written *.txt files in a directory
Can you arrange so the files are written into folder with a date in the name? Like YYYYMMDD ?
Re: Copy the last 7 written *.txt files in a directory
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
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

Re: Copy the last 7 written *.txt files in a directory
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.
Re: Copy the last 7 written *.txt files in a directory
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.

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.
Re: Copy the last 7 written *.txt files in a directory
What about keeping a certain number of the most recent files? say 50?
Re: Copy the last 7 written *.txt files in a directory
Like this: to keep the most recent 7 txt files.
Make sure you test it on sample 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
Re: Copy the last 7 written *.txt files in a directory
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
)
Re: Copy the last 7 written *.txt files in a directory
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
Re: Copy the last 7 written *.txt files in a directory
oops... edit... trying now..
Re: Copy the last 7 written *.txt files in a directory
Worked like a charm... setlocal enabledelayedexpansion was missing for the counter to work.
I appreciate all your time and effort!
I appreciate all your time and effort!