Jump to content

Clear Hard Drive Space Automatically


Hadez
 Share

Recommended Posts

Im working on a script that will auto delete files when the hdd space on the drive gets below 10gb.Once if sees that the free hdd space falls below 10gb, The script will check the folder i want it to delete files from, check the creation date of each file and if the file is over 5 days old, then delete it.

Im having some problems with the checking of files. Can anyone give some guidance?

ive sort of got the search function for the files i want to delete, and ive got the date i want to delete the fiels that were created early than but am strugglin to join the two. Any help would be appreciated. So in summary:

Drive space will be watched

If Free Drive space goes below 10gb, the folder D:\Files\ is searched

All files in D:\Files\ that are older than 5 days old are deleted

Process is looped.

#include <GuiConstantsEx.au3>
#include <Date.au3>
#include <WindowsConstants.au3>

    Global $iMemo
    Local $FREE, $search, $hGUI, $tFile, $aFile, $sFile

    ; Create GUI
    ;$hGUI = GUICreate("Time",400,400)
    ;$iMemo = GUICtrlCreateEdit("", 2, 2, 396, 396, $WS_VSCROLL)
    $hGUI = GUICreate("Drive Space", 469, 639, (@DesktopWidth - 469) / 2, (@DesktopHeight - 639) / 2, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)
    $iMemo = GUICtrlCreateEdit("", 2, 2, 465, 635, $WS_VSCROLL)
    GUICtrlSetFont($iMemo,9, 400, 0, "Courier New")
    GUISetState()

While 1
    FREEDRIVESPACE()
WEnd

Func FREEDRIVESPACE()

    $FREE = DriveSpaceFree("C:\")

;Shows the filenames of all files in the current directory.

if $FREE < 10480.00 then
$search = FileFindFirstFile("D:\Files\" & "*")
; Check if the search was successful
    If $search = -1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        Sleep(10000)
        Return
    EndIf

    While 1
        $file = FileFindNextFile($search)
        $t =  FileGetTime("D:\File\" & $file, 1)
        if not @error then
        $yyyymd = $t[0] & "/" & $t[1] & "/" & $t[2] ; getting errors on this once list completes
        endif
        ;If @error Then ExitLoop
        if $file = "" then Return
        MemoWrite("File: .: " & $file)
        MemoWrite("CREATION DATE .: " & $yyyymd)
    sleep(10)
    WEnd

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    ; Close the search handle
    FileClose($search)
elseif $FREE > 10480.00 then
    MsgBox(0,"TEST", "You have " & $FREE & " free space", 0)
    sleep(1000)
    Return
Endif
EndFunc

Func MemoWrite($sMessage)
    GUICtrlSetData($iMemo, $sMessage & @CRLF, 1)
EndFunc   ;==>MemoWrite

;######### Finds Date Roughly five days before Todays date ################

$Dayminus5 = @MDAY - 5
    if $Dayminus5 = 0 then $Dayminus5 = -1
        if $Dayminus5 < 0 Then
            $NewDay = 31 - SQRT(($Dayminus5)*($Dayminus5))
            if $NewDay = 0 then
                $NewDay = 31
            Endif
            $NewMonth = @MON - 1
            if $NewMonth = 0 then
                $NewMonth = 12
                $NewYear = @YEAR - 1
            Endif
            $NewYear = @YEAR
        Endif
        If $Dayminus5 > 0 Then
            $NewDay = $Dayminus5
            $NewMonth = @MON
            $NewYear =  @YEAR
        Endif

MsgBox(0, "New Date", $NewYear & "/" & $NewMonth & "/" & $NewDay)
Edited by Hadez
Link to comment
Share on other sites

I'm too tired to integrate it into what you already have, but give this function a try, making sure you keep the '#include <Date.au3>' in the script. If you call this function, it'll start the search, compare each file date to the current date on the fly, and if it's 5 days or older, delete it. Certainly play around with it and add your memo write where needed, but I think this should do the trick for ya. The key was all in the _DateDiff function, and once I figured out how to use it it worked great. You can fine tune it by searching by days, hours or even minutes if you want. Looks like all you need to do is have it call on this when the HD size is down to the trigger size. If you want it to only keep going until you free up enough space, I would look into adding an if statement at the end of the while loop that checks the HD space again (at that point I'd make it a function); once it gets to y size, exit loop.

Hope this helps.

Func _deleteOld()
$dir = "D:\Files\"

$search = FileFindFirstFile($dir & "*.*") ; get all files from the directory

If $search = -1 Then ; return an error if the search fails
    MsgBox(0, "Error", "No files found in the search")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    $t =  FileGetTime($dir & $file, 1) ; Pulls the file creation time into an array, use 0 for a string
    $tformatted = $t[0] & "/" &  $t[1] & "/" & $t[2] & " " & $t[3] & ":" & $t[4] & ":" & $t[5] ; Formatted to work with _DateDiff
    $iDateCalc = _DateDiff( 'D', $tformatted,_NowCalc()) ;throws the difference of the time in Days into the variable
    if $iDateCalc > 4 Then filedelete($dir & $file) ;If the value, returned in days, is greater than 4 (5 or higher) delete the file
WEnd

FileClose($search)

EndFunc ;==> _deleteOld()
Link to comment
Share on other sites

@Hadez

Have a read of the comments. I suspect it is something simple

#include <GuiConstantsEx.au3>
#include <Date.au3>
#include <WindowsConstants.au3>
;~ added the below two lines
Opt("MustDeclareVars", 1)
#AutoIt3Wrapper_Run_Debug_Mode=Y

Global $iMemo
Local $FREE, $search, $hGUI, $tFile, $aFile, $sFile

; Create GUI
;$hGUI = GUICreate("Time",400,400)
;$iMemo = GUICtrlCreateEdit("", 2, 2, 396, 396, $WS_VSCROLL)
$hGUI = GUICreate("Drive Space", 469, 639, (@DesktopWidth - 469) / 2, (@DesktopHeight - 639) / 2, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)
$iMemo = GUICtrlCreateEdit("", 2, 2, 465, 635, $WS_VSCROLL)
GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
GUISetState()

While 1
    FREEDRIVESPACE()
WEnd
;~ This code does not get run as the while loop will not be broken.

$Dayminus5 = @MDAY - 5
If $Dayminus5 = 0 Then $Dayminus5 = -1
If $Dayminus5 < 0 Then
    $NewDay = 31 - Sqrt(($Dayminus5) * ($Dayminus5))
    If $NewDay = 0 Then
        $NewDay = 31
    EndIf
    $NewMonth = @MON - 1
    If $NewMonth = 0 Then
        $NewMonth = 12
        $NewYear = @YEAR - 1
    EndIf
    $NewYear = @YEAR
EndIf
If $Dayminus5 > 0 Then
    $NewDay = $Dayminus5
    $NewMonth = @MON
    $NewYear = @YEAR
EndIf

MsgBox(0, "New Date", $NewYear & "/" & $NewMonth & "/" & $NewDay)

Func FREEDRIVESPACE()

    $FREE = DriveSpaceFree("C:\")

    ;Shows the filenames of all files in the current directory.

    If $FREE < 10480.00 Then
        $search = FileFindFirstFile("D:\Files\" & "*")
        ; Check if the search was successful
        If $search = -1 Then
            MsgBox(0, "Error", "No files/directories matched the search pattern")
            Sleep(10000)
            Return
        EndIf

        While 1
            $file = FileFindNextFile($search)
            $t = FileGetTime("D:\File\" & $file, 1)
            If Not @error Then
;~              use _ArrayDisplay tio see what is going on.
;~              $yyymd is not defined
                $yyyymd = $t[0] & "/" & $t[1] & "/" & $t[2] ; getting errors on this once list completes.
            EndIf
            ;If @error Then ExitLoop
            If $file = "" Then Return
            MemoWrite("File: .: " & $file)
            MemoWrite("CREATION DATE .: " & $yyyymd)
            Sleep(10)
        WEnd

        Do
        Until GUIGetMsg() = $GUI_EVENT_CLOSE
        ; Close the search handle
        FileClose($search)
    ElseIf $FREE > 10480.00 Then
        MsgBox(0, "TEST", "You have " & $FREE & " free space", 0)
        Sleep(1000)
        Return
    EndIf
EndFunc   ;==>FREEDRIVESPACE

Func MemoWrite($sMessage)
    GUICtrlSetData($iMemo, $sMessage & @CRLF, 1)
EndFunc   ;==>MemoWrite

;######### Finds Date Roughly five days before Todays date ################

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

Thanks for your help. Think its all done now :graduated: Awesome :(

#include <GuiConstantsEx.au3>
#include <Date.au3>
#include <WindowsConstants.au3>

while 1
_deleteOld()
wend

Func _deleteOld()

    $FREE = DriveSpaceFree("C:\")

    if $FREE > 10240.00 then
        MsgBox(0,"TEST", "You have " & $FREE & " free space",1)
        sleep(360000)
    Return
    Endif
    if $FREE < 10240.00 Then

        $dir = "D:\Files\"

        $search = FileFindFirstFile($dir & "*.*") ; get all files from the directory

        If $search = -1 Then ; return an error if the search fails
            MsgBox(0, "Error", "No files found in the search")
            Exit
        EndIf

        While 1
            $file = FileFindNextFile($search)
            If @error Then ExitLoop
            $t =  FileGetTime($dir & $file, 1) ; Pulls the file creation time into an array, use 0 for a string
            $tformatted = $t[0] & "/" &  $t[1] & "/" & $t[2] & " " & $t[3] & ":" & $t[4] & ":" & $t[5] ; Formatted to work with _DateDiff
            $iDateCalc = _DateDiff( 'D', $tformatted,_NowCalc()) ;throws the difference of the time in Days into the variable
            if $iDateCalc > 4 Then filedelete($dir & $file) ;If the value, returned in days, is greater than 4 (5 or higher) delete the file
            Sleep(100)
        WEnd

        FileClose($search)
    Endif

EndFunc ;==> _deleteOld()
Edited by Hadez
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...