Kaseya Community

Script To Delete All But The Most Recent Files In A Folder

This question is answered

We have a customer that has a proprietary line-of-business application that performs automatic nightly backups to a pre-designated folder. These are zip files that range from hundreds of megabytes to several gigabytes in size. We have been manually deleting the oldest backups every few weeks to keep the hard drive from filling up. We'd like a way to script this procedure in Kaseya. Any ideas?

 

 

Verified Answer
  • Do you want to delete files older than a certain date?  If so, forfiles is your best (and easiest) bet.  You'll have to push forfiles.exe to any XP machine that doesn't have the devel tools installed, as it doesn't come with XP:

    Using forfiles to delete files older than 5 days:


    forfiles -p "c:\some\path" -m "*.log" -d -5 -c "cmd /c del \"@PATH\@FILE\""

    If you don't care about the date, you can just keep the latest X number of files without using forfiles, and without having to install forfiles on an XP box:

    To delete everything in a directory except the latest 5 files:


    for /f "skip=5 delims=" %a in ('dir "c:\some\path" /b /o-d /a-d') do del %a

    If you don't want to push forfiles.exe, you can use the vbscript options above.

    Please test any of these on a directory you don't mind destroying before you put them in production.  While I don't see why my two examples wouldn't work, I have NOT tested them.

All Replies
  • Forfiles!

    technet.microsoft.com/.../cc753551(WS.10).aspx

  • there are a few VB scripts floating about that could also do this;


    OPTION EXPLICIT
    DIM strExtensionsToDelete,strFolder
    DIM objFSO

    ' ************************************************************
    ' Setup
    ' ************************************************************

    ' Folder to delete files from (files will also be deleted from subfolders)
    strFolder = wscript.arguments(0)                 ' Example: <Drive>:\<Path>
    ' A comma separated list of file extensions
    ' Files with extensions provided in the list below will be deleted
    strExtensionsToDelete = wscript.arguments(1)    ' Example: "wav,avi,mp3"

    ' ************************************************************

    set objFSO = createobject("Scripting.FileSystemObject")

    RecursiveDeleteByExtension strFolder,strExtensionsToDelete

    wscript.echo "Finished"

    sub RecursiveDeleteByExtension(byval strDirectory,strExtensionsToDelete)
        DIM objFolder, objSubFolder, objFile
        DIM strExt

        set objFolder = objFSO.GetFolder(strDirectory)
        for each objFile in objFolder.Files
            for each strExt in SPLIT(UCASE(strExtensionsToDelete),",")
                if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
                    wscript.echo "Deleting:" & objFile.Path
                    objFile.Delete
                    exit for
                end if
            next
        next   
        for each objSubFolder in objFolder.SubFolders
            RecursiveDeleteByExtension objSubFolder.Path,strExtensionsToDelete
        next
    end sub

  • I use a variation of what HardKnox posted. My VBScript deletes any files older than my specified date (in this script it's more than 1 day old):

    Dim fso, f, f1, fc,

    Set fso = CreateObject("Scripting.FileSystemObject")

    Path = "Path to folder with files to delete
    Set f = fso.GetFolder(Path)
    Set fc = f.Files

    For Each f1 in fc  If f1.DateLastModified <= date() - 1 Then ' this deletes files older than one day past today's date - change as needed
       fso.DeleteFile(f1)
    next

  • Do you want to delete files older than a certain date?  If so, forfiles is your best (and easiest) bet.  You'll have to push forfiles.exe to any XP machine that doesn't have the devel tools installed, as it doesn't come with XP:

    Using forfiles to delete files older than 5 days:


    forfiles -p "c:\some\path" -m "*.log" -d -5 -c "cmd /c del \"@PATH\@FILE\""

    If you don't care about the date, you can just keep the latest X number of files without using forfiles, and without having to install forfiles on an XP box:

    To delete everything in a directory except the latest 5 files:


    for /f "skip=5 delims=" %a in ('dir "c:\some\path" /b /o-d /a-d') do del %a

    If you don't want to push forfiles.exe, you can use the vbscript options above.

    Please test any of these on a directory you don't mind destroying before you put them in production.  While I don't see why my two examples wouldn't work, I have NOT tested them.

  • Thank you for all of the quick replies! Dan's response exactly matches the two options I wish to use.