Need a way to delete files from a directory, I only want to keep the newest 40 files

I have a program that downloads images every 6 minutes, and the directory grows to be huge and stalls out the computer.   but I only want to keep the newest images, I only want to keep 30 of the newest images.  

I want a simple .bat script that I will call with a cron job, that would check the directory c:/satelit and delete  the older files   they are time stampe

cctaco2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vnicolaeCommented:
Try this:

cd/d C:\your_directory
for /f "skip=30 delims=" %i in ('dir /b /od') do del "%i"


Hope it helps.


Vlad
0
AmazingTechCommented:
Try this.
Set NumOfFilesToKeep=30
Set Directory=C:\Files
 
FOR /f "Skip=%NumofFilesToKeep% tokens=*" %%a in ('dir /a-d /o-d /b "%Directory%"') do ECHO DEL /q "%Directory%\%%a"

Open in new window

0
cctaco2Author Commented:
cd/d C:\processed_satelite
for /f "skip=100 delims=" %i in ('dir /b /od') do del "%i"


I placed the script into notepad and saved it as a process.bat and it didn't see to work properly

did I miss something, I'm skipping I think 100 of the newestfiles?
0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

cctaco2Author Commented:
i typed in the commands and got a error

in not expected at this time
0
vnicolaeCommented:
I tried it on XP and it worked. What OS are you using?
0
cctaco2Author Commented:
Amazaing tech,

Set NumOfFilesToKeep=10
Set Directory=C:\processed_satelite
 
FOR /f "Skip=%NumofFilesToKeep% tokens=*" %%a in ('dir /a-d /o-d /b "%Directory%"') do ECHO DEL /q "%Directory%\%%a"

It's not removing the files from the directory, I see it create a ms-dos box and the commands just fly by and then it closes.

This may have some bearing on it the file name is

2008.09.21.1415.gensat.png

thanks
0
vnicolaeCommented:
You have to remove the 'echo' from AmazingTech's solution...
0
cctaco2Author Commented:
I'm using windows xp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.