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
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.
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
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.
Summary: What I am aiming to do is compare the server backup to the workstation backup. The reason we are doing this is because this: Lets say workstation user has a folder with 10 files in it. The backup ru...
Summary: I am attempting to create a batch file to search for a specific file on my computer and delete it. I have been trying to figure it out and cant get it. Thank You for any help! ...
Summary: I found this code and tried to modify it for my purposes. I wanted to find the word "cookie" in some text based files and then delete the file if found. If the word "cookie" is on its own line, the f...