Link to home
Start Free TrialLog in
Avatar of cctaco2
cctaco2

asked on

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

Avatar of vnicolae
vnicolae

Try this:

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


Hope it helps.


Vlad
SOLUTION
Avatar of AmazingTech
AmazingTech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cctaco2

ASKER

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?
Avatar of cctaco2

ASKER

i typed in the commands and got a error

in not expected at this time
I tried it on XP and it worked. What OS are you using?
Avatar of cctaco2

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial