Link to home
Start Free TrialLog in
Avatar of shahzamm
shahzammFlag for Saudi Arabia

asked on

Low Hard Drive Space Notification

Hi all

we are in Domain enviroment. We need to implement an idea of "Low disk Space Notification". The Idea is when any of the drives/volumes reaches a specified limit, it should notify the user.
can this be enhanced, I mean it scans the drive for large folders/files & notify the owner of that folder/file.

I need advise from experts on this, how we can achive this in best way. (thourgh SCCM or login script a utility etc..)
Avatar of jimsweb
jimsweb
Flag of India image

Hope this helps:
usageLog = "C:\UsageLog.txt"
minCapacity = 0.05	' 5%
 
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set oDrive = fso.GetDrive("C")
curSpace = oDrive.FreeSpace
totSize = oDrive.TotalSize
 
curUsage = curSpace/totSize
curFree = 1 - curUsage
WScript.Echo "Current free space: " & FormatNumber(oDrive.FreeSpace,0) & vbLf & _
		   "Total size: " & FormatNumber(oDrive.TotalSize,0) & vbLf & _
		   "Current usage(%): " & FormatPercent(curUsage, 2) & vbLf & _
		   "Current free(%): " & FormatPercent(curFree, 2)
 
 
'-------------------------
' Check the capacity here
'-------------------------
if curFree < minCapacity then
	WScript.Echo "Too low!"
 
	' write log file here
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set f = fso.CreateTextFile(usageLog, True)
	f.WriteLine("Space too low!")
	f.Close
else
	WScript.Echo "OK!"
end if

Open in new window



credit: sungenwang
Avatar of shahzamm

ASKER

its showing the current status. is it possible if we need to use it as login script & it notifies only when there is low space, not one every login?
its showing space in bytes, while we can live with percentage and/or GBs.
it does not show which drive is having what space
Avatar of nobus
Thanks guys for quick response
we are not ready yet for powershell. still using VB..any suggestions very much appriciated..
sorry - it's all i found
anyone else form community shed some light please
Hi shahzamm,

Below is a sample of a simple function I wrote some years ago.

The script basically gets the free space for the selected drive into a variable and outputs to the screen in a message box.  If you already have VBScripts that you use at logon, you should be able to very easily incorporate the function into an existing script.

Hope this helps,
Daz.

'Check space on C drive

sDriveToCheck = "C"
sSpace = fShowFreeSpace(sDriveToCheck)

MsgBox "Free space on " & sDriveToCheck & " = " & sSpace & " GB",64 , "C Drive Free Space Result"



Function fShowFreeSpace(strDrive)
    Dim fso, d, s
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set d = fso.GetDrive(strDrive)
    s = Round(d.FreeSpace / (1024 * 1024 * 1024), 2)
    fShowFreeSpace = s
End Function

Open in new window

Its really simple, this is what I am looking
sorry for asking more but I am not developing guru.
can it be modified
1. run at each login
2. if drive space is below 10% then notify...
3. else don't notify.

Add the following to your existing logon script (or get your logon script to call it).  I have assumed that you only want the 'C' drive checked.

If the drive has more than 10% free, the script will not output anything.  If you want to test it, play with the line:
iThresholdPercent = 10

Just to comment on the other possible requirement in your original post, scanning the drive for large files (or any files) is NOT advisable in a logon script as this could hold up the logon for many minutes.

Hope this helps
Daz.
'Check space on C drive

Dim sDriveToCheck, iThresholdPercent, sSpacePercent

sDriveToCheck = "C"
iThresholdPercent = 10

sSpacePercent = fGetFreeSpace(sDriveToCheck)

If sSpacePercent < iThresholdPercent Then
    MsgBox "W A R N I N G:  Your '" & sDriveToCheck & "' drive is " & 100 - sSpacePercent & "% full.  Please delete any unwanted files as soon as possible.", vbExclamation + vbSystemModal, "Drive '" & sDriveToCheck & "' free space Warning!"
End If


Function fGetFreeSpace(strDrive)
    Dim fso, oDrive, dblSpace, dblTotalSize, temp, iPercent
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oDrive = fso.GetDrive(strDrive)
    dblTotalSize = oDrive.TotalSize
    dblSpace = oDrive.FreeSpace
    temp = dblSpace / dblTotalSize * 100
    iPercent = CInt(temp)
    fGetFreeSpace = iPercent
End Function

Open in new window

Just one last thing..not sure if doable
fyi: we have one HDD with C: & D: partitions. user profile are in D: & system is in C:

a script which run with a delay after login
check the current user profile size. warn if size is more than 50% of total partition size (D:).

This will reduce the burdon over IT support to run a utility & then find which folder is taking space

Thank you very much for your support
Possible solution for your additional requirement:

1. save the following in a script called 'ProfileCheck.vbs'
Dim intThresholdPercent, intProfilePercent, intPause

intPause = 20000  '# 20 Seconds
WScript.Sleep intPause

intThresholdPercent = 50

intProfilePercent = fGetCurrentUserProfilePercent()

If intProfilePercent > intThresholdPercent Then
    MsgBox "W A R N I N G:  Your profile is using " & intProfilePercent & "% of the volume.", vbExclamation + vbSystemModal, "Profile size Warning!"
End If

Function fGetCurrentUserProfilePercent()
    Dim fso, wshShell, oDrive, sProfileFolder, oProfileFolder, sProfileDrive, dblProfileSize, dblTotalSize, temp, iPercent
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set wshShell = CreateObject("WScript.Shell")
    sProfileFolder = wshShell.ExpandEnvironmentStrings("%USERPROFILE%")
    sProfileDrive = Left(sProfileFolder, 1)
    Set oProfileFolder = fso.GetFolder(sProfileFolder)
    Set oDrive = fso.GetDrive(sProfileDrive)
    dblTotalSize = oDrive.TotalSize
    dblProfileSize = oProfileFolder.Size
    temp = dblProfileSize / dblTotalSize * 100
    iPercent = CInt(temp)
    fGetCurrentUserProfilePercent = iPercent
End Function

Open in new window


2. Add the following lines into one of your VBScript logon scripts:
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run "WScript.exe ""Path\To\ProfileCheck.vbs""", 1, False

Open in new window


The 'False' in the line above means that calling the ProfileCheck.vbs will not halt the logon script.

Regards,
Daz
@Daz
its working on some machines perfectly with an error on 28, 5. permission denied.. but on some its just the error 28,5 permission denied. what could be this
Hi,

What is giving the error, drive free space check or profile size check?

Could you please post the script that giving the error, and identify what is line 28 (if it is not obvious)?

Regards,
Daz.
attached is the error for your script for the script below. its your provided script but I changed size limit to test.

Dim intThresholdPercent, intProfilePercent, intPause

intPause = 20000  '# 20 Seconds
WScript.Sleep intPause

intThresholdPercent = 90

intProfilePercent = fGetCurrentUserProfilePercent()

If intProfilePercent > intThresholdPercent Then
    MsgBox "W A R N I N G:  Your profile is using " & intProfilePercent & "% of the volume.", vbExclamation + vbSystemModal, "Profile size Warning!"
End If

Function fGetCurrentUserProfilePercent()
    Dim fso, wshShell, oDrive, sProfileFolder, oProfileFolder, sProfileDrive, dblProfileSize, dblTotalSize, temp, iPercent
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set wshShell = CreateObject("WScript.Shell")
    sProfileFolder = wshShell.ExpandEnvironmentStrings("%USERPROFILE%")
    sProfileDrive = Left(sProfileFolder, 1)
    Set oProfileFolder = fso.GetFolder(sProfileFolder)
    Set oDrive = fso.GetDrive(sProfileDrive)
    dblTotalSize = oDrive.TotalSize
    dblProfileSize = oProfileFolder.Size
    temp = dblProfileSize / dblTotalSize * 100
    iPercent = CInt(temp)
    fGetCurrentUserProfilePercent = iPercent
End Function

scripterror.JPG
Hi,

My script works as a non-admin user on XP.

You did not say what OS, but from your screenshot of the error it is a flavour of Vista or Windows 7.  I do not have Vista, but will fire my Windows 7 VM to have a look.

What OS are you using and is it 32 or 64 bit?

Daz
32 bit vista & win 7
ASKER CERTIFIED SOLUTION
Avatar of Darren Collins
Darren Collins
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Daz..Exactly what I was looking for