• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

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

0
cctaco2
Asked:
cctaco2
  • 4
  • 3
3 Solutions
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now