Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 09, 2016, 01:22:44 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: App/code to limit the number of files in a folder, auto-deleting older ones  (Read 940 times)

paulobrabo

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 88
  • The Brazilian Bomber
    • View Profile
    • Brabo Illustration
    • Donate to Member
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.co...our-self+cleaning-pc


English will never be my first language, it doesn't meter how hard I try.
« Last Edit: February 13, 2013, 08:42:02 AM by paulobrabo »

paulobrabo

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 88
  • The Brazilian Bomber
    • View Profile
    • Brabo Illustration
    • Donate to Member
Aha, found the answer here:
http://forums.majorg...wthread.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.
English will never be my first language, it doesn't meter how hard I try.
« Last Edit: February 13, 2013, 02:37:25 PM by paulobrabo »