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?
 
cctaco2Connect With a Mentor Author Commented:
I'm using windows xp
0
 
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
 
AmazingTechConnect With a Mentor Commented:
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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
 
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
 
vnicolaeConnect With a Mentor Commented:
You have to remove the 'echo' from AmazingTech's solution...
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.