ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > General Software Discussion

App/code to limit the number of files in a folder, auto-deleting older ones

(1/1)

paulobrabo:
Hi friends, help needed!

Here's the setup: I use Macrium Reflect to make daily images of my system drive, which I save in a backup folder in two different backup locations. My problem is that the folder where I save these images gets huge really fast, and I want to limit the number of saved images to, say, 4 or 7. I currently use Belvedere to delete files *older than* – but that can be a problem if, for instance, I travel and spend one month without using the PC (and so without creating new automatic backup images). When I come back and turn on the PC all images will be deleted because *older than*, and I will be left without any backup image.

What I'm looking for is an app or code that may limit the number of files in my backup folder. Something like "if there is more than 'n' files in this folder, delete the oldest file and keep the 'n' newest ones". I'm sure there's more than an easy way to do this, but I can't code and my searches led me to nothing.

Thank you for any hints! Windows 7 Home Premium.

Paulo

http://lifehacker.com/341950/belvedere-automates-your-self+cleaning-pc


paulobrabo:
Aha, found the answer here:
http://forums.majorgeeks.com/showthread.php?t=197158  8)

I saved the code below as a .bat file, which must then be run as administrator. The number after "skip" is the number of files you want to keep. Older files in the folder that exceed that number will be deleted.


--- ---@echo off &setlocal
set folder=C:\desired-folder-name

pushd "%folder%"
for /f "skip=3 tokens=*" %%i in ('dir /a:-d-s /b /o:-d') do del "%%i"
popd
Needless to say, use with the utmost caution. Somewhere, somehow, something's gonna be deleted.

Navigation

[0] Message Index

Go to full version