Count files and delete oldest

February 25, 2009 at 12:53:39
Specs: Windows XP
I need a script that counts the number of files in a folder and when the count reaches 8, the oldest file in the folder is removed.

Thanks.


See More: Count files and delete oldest

Report •


#1
February 25, 2009 at 13:59:54
What do you mean "oldest"? Files have a Creation date, Last Accessed date and a Last Written date. Which one is to be used?

Report •

#2
February 25, 2009 at 20:34:46
This will delete the oldest file from current directory if there are more than 8 entries in the current directory.
set file2del=
for /f "skip=8" %%A in ('dir /b/o-d') do set file2del=%%A
if not "%file2del%"=="" del "%file2del%"

--
Holla.


Report •

#3
February 25, 2009 at 23:01:05
Holla,

I think you can nevermind the if:

+++++++++++++++++++++++++++++++++++
@echo off & setLocal EnableDelayedExpansion

for /f "skip=7 tokens=* delims= " %%a in ('dir/b/o-d') do (
echo del %%a
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
February 26, 2009 at 01:05:58
M2,

well, the requirements is not clear.
I thought he wants to delete only the oldest
file. I mean delete just one file.
I think your solution deletes all files except the latest 7. Right?

[
I often think that if only questioners paid
enough attention to details ... as much
attention as paid by the respondents... :-( ....
]

--
Holla.


Report •

#5
February 26, 2009 at 05:53:57
Sorry about not being more precise with my question. I need to delete the oldest file based on the creation date as sort of a file retention policy (I can only allow 7 files in the folder). I didn't get a chance to check the posts until this morning so I will try what is listed and report back with results.

Report •

#6
February 26, 2009 at 08:34:07
Change this:

('dir/b/o-d')

to:

('dir/b/o-d/tc')


=====================================
If at first you don't succeed, you're about average.

M2


Report •


Ask Question