Computing.Net > Forums > Programming > Count files and delete oldest

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Count files and delete oldest

Reply to Message Icon

Name: aheusdens
Date: February 25, 2009 at 12:53:39 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Valerie (by Garibaldi)
Date: February 25, 2009 at 13:59:54 Pacific
Reply:

What do you mean "oldest"? Files have a Creation date, Last Accessed date and a Last Written date. Which one is to be used?


0

Response Number 2
Name: Holla
Date: February 25, 2009 at 20:34:46 Pacific
Reply:

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.


0

Response Number 3
Name: Mechanix2Go
Date: February 25, 2009 at 23:01:05 Pacific
Reply:

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


0

Response Number 4
Name: Holla
Date: February 26, 2009 at 01:05:58 Pacific
Reply:

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.


0

Response Number 5
Name: aheusdens
Date: February 26, 2009 at 05:53:57 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: February 26, 2009 at 08:34:07 Pacific
Reply:

Change this:

('dir/b/o-d')

to:

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


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

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon

batch app read line from ... Batch file to ADD WIRELES...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Count files and delete oldest

Need batch file to delete old files www.computing.net/answers/programming/need-batch-file-to-delete-old-files/14092.html

Batch File search and delete file www.computing.net/answers/programming/batch-file-search-and-delete-file/16386.html

search and delete batch file www.computing.net/answers/programming/search-and-delete-batch-file/16412.html