Skip to content

Instantly share code, notes, and snippets.

@te-online
Forked from numeralnathan/Delete Backups.au3
Created April 4, 2016 07:51
Show Gist options
  • Save te-online/eb6be6031f2fdefff22a306adbbe9bd6 to your computer and use it in GitHub Desktop.
Save te-online/eb6be6031f2fdefff22a306adbbe9bd6 to your computer and use it in GitHub Desktop.
An AutoIt script to delete Windows Backups when disk space falls below threshold
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
; This is where the backups are stored
Global Const $BACKUP_DIR = "G:\NREYNOLD-LAP"
$free = DriveSpaceFree($BACKUP_DIR)
; Don't delete backups if more than 60 GB is available (i.e. enough for another new backup directory to be created)
if $free > 60 * 1024 then
MsgBox($MB_OK, "Delete Backups", "No backups deleted since there is " & Floor($free / 1024) & " GB of free space")
exit(0)
endif
; G:\machine_name is where the backups are stored
$find = FileFindFirstFile($BACKUP_DIR & "\Backup Set *")
if $find = -1 then
MsgBox(0, "Delete Backups", "Unable to find backups at " & $BACKUP_DIR)
exit(1)
endif
$canidate = "ZZZZZ"
$count = 0
while true
$file = FileFindNextFile($find)
if @error then ExitLoop
if StringCompare($canidate, $file) > 0 then $canidate = $file
$count = $count + 1
wend
FileClose($find)
$response = MsgBox($MB_YESNO, "Delete Backups", "There is " & Floor($free / 1024) & " GB of free space." & @CRLF & "Delete the backup: " & $canidate & "?")
if $response <> $IDYES then Exit
$result = DirRemove($BACKUP_DIR & "\" & $canidate, $DIR_REMOVE)
if $result = 0 then
MsgBox(0, "Delete Backups", "Failed to delete " & $canidate)
else
MsgBox(0, "Delete Backups", "Deleted " & $canidate)
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment