Posted By

RobertHirabayashi on 02/14/12


Tagged

space free letter filesystemobject available disk hard drive


Versions (?)

FreeDiskSpace Function


 / Published in: Visual Basic
 

URL: http://www.alanphipps.com/VBScript-DiskFile.html#FreeSpace

Adapted from URL above. This function can easily be adapted to output the integer value of available drive space in kilo, mega or gigabytes.

  1. Function FreeDiskSpace(DriveLetter)
  2. ' Returns the number of free Megabytes on a given drive
  3. ' http://www.alanphipps.com/VBScript-DiskFile.html
  4. ' Instantiate the VBScript FileSystemObject
  5. Set FsoObject = WScript.CreateObject("Scripting.FileSystemObject")
  6.  
  7. ' Use the FilesystemObject Object's GetDrive Method
  8. Set DiskDrive = FsoObject.GetDrive(FsoObject.GetDriveName(DriveLetter & ":"))
  9.  
  10. ' Main Processing Section
  11.  
  12. ' Use the FileSystemObjects FreeSpace Property to Determine the Amount of FreeSpace in MB
  13. ' on the C: Drive
  14.  
  15. AvailSpacebytes = DiskDrive.FreeSpace
  16. AvailSpaceKB = DiskDrive.FreeSpace / 1024
  17. AvailSpaceMB = (DiskDrive.FreeSpace / 1024) / 1024
  18. AvailSpaceGB = ((DiskDrive.FreeSpace / 1024) / 1024) / 1024
  19.  
  20. ' Use VBScripts FormatNumber Function to format the results as a Whole Number
  21. AvailSpacebytes = FormatNumber(AvailSpacebytes, 0)
  22. AvailSpaceKB = FormatNumber(AvailSpaceKB, 0)
  23. AvailSpaceMB = FormatNumber(AvailSpaceMB, 0)
  24. AvailSpaceGB = FormatNumber(AvailSpaceGB, 0)
  25.  
  26. ' Display the Amount of Free Space on the C: Drive
  27. WScript.Echo "There is Currently: " & AvailSpacebytes & " bytes available on the C: Drive" & vbCrLf & _
  28. "There is Currently: " & AvailSpaceKB & "KB available on the C: Drive" & vbCrLf & _
  29. "There is Currently: " & AvailSpaceMB & "MB available on the C: Drive" & vbCrLf & _
  30. "There is Currently: " & AvailSpaceGB & "GB available on the C: Drive" & vbCrLf
  31.  
  32. FreeDiskSpace = AvailSpaceMB
  33. End Function

Report this snippet  

You need to login to post a comment.