Script to keep a fixed maximum number of iles in a directory

Hi,

Is there a way to make a script for Windows that will keep only a fixed number of files in a directory, no matter what is the files time and date stamps ? like 10 copies

fox54Asked:
Who is Participating?
 
Bill PrewCommented:
Here's a simple BAT script that should to it, just change the name of the folder in the PUSHD command.

@echo off
pushd "c:\temp"
for /F "skip=10 tokens=*" %%A in ('dir /b /o-d /a-d') do del "%%~A"
popd

Open in new window

~bp
0
 
Bill PrewCommented:
Do you mean not even allow the files to be created, if so that's not possible.

Or do you mean a script that can delete files if more than 10 exist?  In that case, what criteria would be used to select the 10 to keep?

~bp
0
 
fox54Author Commented:
Just a scrit to delete if more files exists but it has to delete the oldest

So the script need to keep 10 files maximum in the directory, deleting the oldest if there's more than 10.  But if there's less than 10 files in the directory, keep them there no matter what the time and date stamps.
 
0
On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

 
fox54Author Commented:
Thanks

Never heard of those command pushd and popd before
0
 
Steve KnightIT ConsultancyCommented:
Looks like I was too late on this one.  Bill will probably explain in a minute but:

Pushd changes to the directory you say, pretty well the same as cd /d c:\temp but will also map a drive letter if you say chose pushd \\server\share

Popd then undoes your pushd, removes any temporary drive letter and puts you back where you were before.

Steve
0
 
Bill PrewCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.