locked
Deleting files recursively until Free Space is Reached RRS feed

  • Question

  • I am looking for a VBS script that will delete the oldest files on a drive, oldest first until a specified amount of free space is reached.  I've found a script to delete just one files, but several files usually need to be deleted to reach the free space.  Any help is appreciated, since I don't know how to script, and can only edit existing code.  Thanks in advance.  Jeff
    • Moved by Bill_Stewart Wednesday, May 30, 2018 6:36 PM This is not "scripts on demand"
    Sunday, April 15, 2018 4:00 PM

All replies

  • Use PowerShell.  It is easier, faster and not obsolete.

    $volume = 'D'
    $path =  '{0}:\*' -f $volume
    $sizerequired = 10Gb
    $files = Get-ChildItem $path -file -Recurse | sort LastWriteTime
    foreach($file in $files){
        $freespace = (get-volume $volume).SizeRemaining
        if($freespace -lt $sizerequired){
            $file | Remove-Item -Whatif
        }else{
            break
        }
    }



    \_(ツ)_/



    • Edited by jrv Sunday, April 15, 2018 4:26 PM
    • Proposed as answer by I.T Delinquent Monday, April 16, 2018 8:17 AM
    • Marked as answer by jeffzylstra Friday, April 20, 2018 7:01 PM
    • Unmarked as answer by jeffzylstra Tuesday, April 24, 2018 4:07 PM
    Sunday, April 15, 2018 4:24 PM
  • Wow!  That looks so much more concise and powerful than VBS!  Thank you for that. 

    The drive is F:, and the folder where files are stored is F:\Backups.  I tried editing the script and running it, but I'm getting all kinds of errors.  I have a feeling it is in the statement that sets the path variable, since I don't understand the path variable above.  I tried 

    $path =  'F:\Backups' -f $volume

    That generated all kinds of errors about files not being found.  Any help is appreciated.  Thank you!

    I only need about 400 Mb of free space.  Thank you again.

    Monday, April 16, 2018 6:43 PM
  • I made a few changes, but I want to make sure I'm doing this correct, since I'm dealing with off-site backup files on a USB drive.  Before making minor changes to the script, I got several errors.  One from the Get-ChildItem $path (instead of -path), and another I think, because there was no colon after the I: when setting the volume variable.  This is a USB drive, by the way, and it's now running on a different computer, so the drive letter is different.  

    I bumped up the $Sizerequired variable to 4000 Gb (it's a 4Tb drive) to try and force it to indicate it's going to delete files (still using the -WhatIf switch, of course), but it just waits a couple seconds, and then returns control.  There's no messages, no errors, nothing but a brief pause.  

    I want to make sure it's not deleting ALL files, just those oldest files necessary to reach my required free space.  I can't tell what it's going to do because there's no errors and no messages.  Thank you again, I.T. Delinquent, for all your help.  It's been educational!  Also, make sure you're running the LATEST version of Powershell, as only version 1 is installed on Windows 7.  I'm now running Powershell 6.1, and many more commands are supported.

    Also, what does this mean?  '{0}:\*' -f $volume

    $volume = 'I:'
    $path =  '{0}:\*' -f $volume
    $sizerequired = 4000Gb
    $files = Get-ChildItem -path -file -Recurse | sort LastWriteTime
    foreach($file in $files){
        $freespace = (get-variable $volume).SizeRemaining
        if($freespace -lt $sizerequired){
            $file | Remove-Item -WhatIf
        }else{
            break
        }
    }


    Friday, April 20, 2018 7:03 PM