Sign in to follow this  
Followers 0
SuperFletch

Space on a Hard Drive

3 posts in this topic

Hi there,

I'm writing a script that runs in a loop and monitors if a hard disk drive drops below a certain amount of free space.

In doing this I use the command DriveSpaceFree("*:\") and then I convert the output to Gb rounded to 2 decimal places as shown below.

$Cspace = DriveSpaceFree("C:\")
$Cspace = $Cspace / 1024
$Cspace = Round($Cspace,2)
Else ;(IF $Ctype is not ("Fixed")
Sleep(20)
EndIf

Ideally what I wanted to do was report when the Drive I'm monitoring dropped below 10 Gb disk space and write that to a log file. Then do the same for 9 Gb disk space and 8 and 7 etc until perhaps 5 Gb where I may begin to email people as well as writing to the log file.

I thought I had some good code to do this which would check when the rounded down value returned by DriveSpaceFree was = to 10.00 or 9.00 or 8.00 etc.

However I have found that if someone copies a large file which changes the amount of disk space free from say 10.01 to 9.99 then because the DriveSpaceFree (rounded) value never reports back as 10.00 the line does not get written to my log file.

I hope this makes sense and someone can help, my code is below.

For clarity what I am trying to do is.

Find disk space free on Local Hard drives.

Convert that to Gb.

Round that to 2 decimal places.

If space is less than or equal to 10.00, 9.00, 8.00 etc Gb

Write a line to a log file (and perform some other actions)

Continue monitoring the situation but do not constantly write to the log file.

Currently:

#include<file.au3>
$1 = 1

$Logfile = (@ScriptDir & "\logfile.txt")
$Ctype = DriveGetType("C:\")
$CWritten_10 = ("No")
$CWritten_9 = ("No")
$CWritten_8 = ("No")
$CWritten_7 = ("No")
$CWritten_6 = ("No")
$CWritten_5 = ("No")
$CWritten_4 = ("No")
$CWritten_3 = ("No")
$CWritten_2 = ("No")
$CWritten_1 = ("No")

Do
Call("DriveSpace_C")

Until $1 = 2



Func DriveSpace_C()

If $Ctype = ("Fixed") then 
$Cspace = DriveSpaceFree("C:\")
$Cspace = $Cspace / 1024
$Cspace = Round($Cspace,2)
Else ;(IF $Ctype is not ("Fixed")
Sleep(20)
EndIf

;######################################################
;10 Gb Warning in Log File
;######################################################

If $Ctype = ("Fixed") then 
If $Cspace = ("10.00") and $CWritten_10 = ("No") then
_FileWriteLog($LogFile,"Only " & $Cspace & " Gb of free space remains on drive C:\.",1)

$CWritten_10 = ("YES")
ElseIf $Cspace = ("10.00") and $CWritten_10 = ("Yes") then
Sleep(20)
Else
$CWritten_10 = ("No")
EndIf
EndIf
;######################################################
;9 Gb Warning in Log File
;######################################################

If $Ctype = ("Fixed") then 
If $Cspace = ("9.00") and $CWritten_9 = ("No") then
_FileWriteLog($LogFile,"Only " & $Cspace & " Gb of free space remains on drive C:\.",1)
$CWritten_9 = ("YES")
ElseIf $Cspace = ("9.00") and $CWritten_9 = ("Yes") then
Sleep(20)
Else
$CWritten_9 = ("No")
EndIf
EndIf
;######################################################
;8 Gb Warning in Log File
;######################################################

If $Ctype = ("Fixed") then 
If $Cspace = ("8.00") and $CWritten_8 = ("No") then
_FileWriteLog($LogFile,"Only " & $Cspace & " Gb of free space remains on drive C:\.",1)
$CWritten_8 = ("YES")
ElseIf $Cspace = ("8.00") and $CWritten_8 = ("Yes") then
Sleep(20)
Else
$CWritten_8 = ("No")
EndIf
EndIf
;######################################################
;7 Gb Warning in Log File
;######################################################

If $Ctype = ("Fixed") then 
If $Cspace = ("7.00") and $CWritten_7 = ("No") then
_FileWriteLog($LogFile,"Only " & $Cspace & " Gb of free space remains on drive C:\.",1)
$CWritten_7 = ("YES")
ElseIf $Cspace = ("7.00") and $CWritten_7 = ("Yes") then
Sleep(20)
Else
$CWritten_7 = ("No")
EndIf
EndIf
;######################################################
;6 Gb Warning in Log File
;######################################################

If $Ctype = ("Fixed") then 
If $Cspace = ("6.00") and $CWritten_6 = ("No") then
_FileWriteLog($LogFile,"Only " & $Cspace & " Gb of free space remains on drive C:\.",1)
$CWritten_6 = ("YES")
ElseIf $Cspace = ("6.00") and $CWritten_6 = ("Yes") then
Sleep(20)
Else
$CWritten_6 = ("No")
EndIf
EndIf
;######################################################
;5 Gb Warning in Log File
;######################################################

If $Ctype = ("Fixed") then 
If $Cspace = ("5.00") and $CWritten_5 = ("No") then
_FileWriteLog($LogFile,"Only " & $Cspace & " Gb of free space remains on drive C:\.",1)
;SEND AN EMAIL

$CWritten_5 = ("YES")
ElseIf $Cspace = ("5.00") and $CWritten_5 = ("Yes") then
Sleep(20)
Else
$CWritten_5 = ("No")
EndIf
EndIf
;######################################################
;4 Gb Warning in Log File
;######################################################

If $Ctype = ("Fixed") then 
If $Cspace = ("4.00") and $CWritten_4 = ("No") then
_FileWriteLog($LogFile,"Only " & $Cspace & " Gb of free space remains on drive C:\.",1)
;SEND AN EMAIL

$CWritten_4 = ("YES")
ElseIf $Cspace = ("4.00") and $CWritten_4 = ("Yes") then
Sleep(20)
Else
$CWritten_4 = ("No")
EndIf
EndIf
;######################################################
;3 Gb Warning in Log File
;######################################################

If $Ctype = ("Fixed") then 
If $Cspace = ("3.00") and $CWritten_3 = ("No") then
_FileWriteLog($LogFile,"Only " & $Cspace & " Gb of free space remains on drive C:\.",1)
;SEND AN EMAIL

$CWritten_3 = ("YES")
ElseIf $Cspace = ("3.00") and $CWritten_3 = ("Yes") then
Sleep(20)
Else
$CWritten_3 = ("No")
EndIf
EndIf
;######################################################
;2 Gb Warning in Log File
;######################################################

If $Ctype = ("Fixed") then 
If $Cspace = ("2.00") and $CWritten_2 = ("No") then
_FileWriteLog($LogFile,"Only " & $Cspace & " Gb of free space remains on drive C:\.",1)
;SEND AN EMAIL
$CWritten_2 = ("YES")
ElseIf $Cspace = ("2.00") and $CWritten_2 = ("Yes") then
Sleep(20)
Else
$CWritten_2 = ("No")
EndIf
EndIf
;######################################################
;1 Gb Warning in Log File
;######################################################

If $Ctype = ("Fixed") then 
If $Cspace = ("1.00") and $CWritten_1 = ("No") then
_FileWriteLog($LogFile,"Only " & $Cspace & " Gb of free space remains on drive C:\.",1)
;SEND AN EMAIL
$CWritten_1 = ("YES")
ElseIf $Cspace = ("1.00") and $CWritten_1 = ("Yes") then
Sleep(20)
Else
$CWritten_1 = ("No")
EndIf




Else ;(IF $Ctype is not ("Fixed")
Sleep(20)
EndIf

EndFunc

Share this post


Link to post
Share on other sites



Rather then checking for

If $Cspace = ("10.00") and $CWritten_10 = ("No") then

you could check if it is less then 10.01

If $Cspace < ("10.01") and $CWritten_10 = ("No") then

another way would be to use switch and cases

Switch $Cspace
    Case 9.01 to 10.00
        ; what to do if between  9.01 and 10.00
    Case 8.01 to 9.00
        ; what to do if between  8.01 and 9.00
    ; ect
EndSwitch

Heres a way to do it using an array.

#include<file.au3>
Global $Logfile = (@ScriptDir & "\logfile.txt"), $sDrive = "C:\", $Ctype = DriveGetType($sDrive), $sWarnAtSizes = "10|9|8|7|6|5|4|3|2|1"
Global $aWarnAtSizes =StringSplit($sWarnAtSizes,"|"), $iEmailThreshold = "7", $iPreviousWarningPos = 1

while 1
    Local $Cspace = DriveSpace()
    If $iPreviousWarningPos > 0 And $Cspace < $aWarnAtSizes[1] Then
        $iPosInArray = _PosInArrayOfSizes($Cspace)
        If $iPosInArray >  $iPreviousWarningPos Then
            _FileWriteLog($LogFile, "Only " & $Cspace & " Gb of free space remains on drive " & $sDrive, 1)
            If $iPosInArray >= $iEmailThreshold Then
                ;SEND AN EMAIL
                MsgBox(0,"send email","")
            EndIf
            $iPreviousWarningPos = $iPosInArray
            If $iPosInArray = 11 Then $iPreviousWarningPos = -1
        EndIf
    EndIf
    Sleep(1000)
WEnd

Func DriveSpace()
If $Ctype = ("Fixed") then
$Cspace = DriveSpaceFree($sDrive)
$Cspace = $Cspace / 1024
$Cspace = Round($Cspace,2)
Return $Cspace
Else ;(IF $Ctype is not ("Fixed")
Sleep(20)
EndIf
EndFunc

Func _PosInArrayOfSizes($Cspace)
    For $i = $iPreviousWarningPos To $aWarnAtSizes[0] Step 1
        If Not ($Cspace <= $aWarnAtSizes[$i]) Then ExitLoop
    Next
    Return $i
EndFunc

GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.

Share this post


Link to post
Share on other sites

Thanks very much for your quick reply - I'll have a look at getting this in place tomorrow.

I'll probably try and do it using the array.

I didn't know about using the to command either which will help I had been trying something similar using < and >.

Thanks immensely.

Share this post


Link to post
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
Sign in to follow this  
Followers 0