Script to keep a fixed maximum number of iles in a directory

Posted on 2011-10-11
Last Modified: 2012-05-12

Is there a way to make a script for Windows that will keep only a fixed number of files in a directory, no matter what is the files time and date stamps ? like 10 copies

Question by:fox54
    LVL 51

    Expert Comment

    by:Bill Prew
    Do you mean not even allow the files to be created, if so that's not possible.

    Or do you mean a script that can delete files if more than 10 exist?  In that case, what criteria would be used to select the 10 to keep?


    Author Comment

    Just a scrit to delete if more files exists but it has to delete the oldest

    So the script need to keep 10 files maximum in the directory, deleting the oldest if there's more than 10.  But if there's less than 10 files in the directory, keep them there no matter what the time and date stamps.
    LVL 51

    Accepted Solution

    Here's a simple BAT script that should to it, just change the name of the folder in the PUSHD command.

    @echo off
    pushd "c:\temp"
    for /F "skip=10 tokens=*" %%A in ('dir /b /o-d /a-d') do del "%%~A"

    Open in new window


    Author Closing Comment


    Never heard of those command pushd and popd before
    LVL 43

    Expert Comment

    by:Steve Knight
    Looks like I was too late on this one.  Bill will probably explain in a minute but:

    Pushd changes to the directory you say, pretty well the same as cd /d c:\temp but will also map a drive letter if you say chose pushd \\server\share

    Popd then undoes your pushd, removes any temporary drive letter and puts you back where you were before.

    LVL 51

    Expert Comment

    by:Bill Prew

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now