/ 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.
Expand |
Embed | Plain Text
Function FreeDiskSpace(DriveLetter) ' Returns the number of free Megabytes on a given drive ' http://www.alanphipps.com/VBScript-DiskFile.html ' Instantiate the VBScript FileSystemObject Set FsoObject = WScript.CreateObject("Scripting.FileSystemObject") ' Use the FilesystemObject Object's GetDrive Method Set DiskDrive = FsoObject.GetDrive(FsoObject.GetDriveName(DriveLetter & ":")) ' Main Processing Section ' Use the FileSystemObjects FreeSpace Property to Determine the Amount of FreeSpace in MB ' on the C: Drive AvailSpacebytes = DiskDrive.FreeSpace AvailSpaceKB = DiskDrive.FreeSpace / 1024 AvailSpaceMB = (DiskDrive.FreeSpace / 1024) / 1024 AvailSpaceGB = ((DiskDrive.FreeSpace / 1024) / 1024) / 1024 ' Use VBScripts FormatNumber Function to format the results as a Whole Number AvailSpacebytes = FormatNumber(AvailSpacebytes, 0) AvailSpaceKB = FormatNumber(AvailSpaceKB, 0) AvailSpaceMB = FormatNumber(AvailSpaceMB, 0) AvailSpaceGB = FormatNumber(AvailSpaceGB, 0) ' Display the Amount of Free Space on the C: Drive WScript.Echo "There is Currently: " & AvailSpacebytes & " bytes available on the C: Drive" & vbCrLf & _ "There is Currently: " & AvailSpaceKB & "KB available on the C: Drive" & vbCrLf & _ "There is Currently: " & AvailSpaceMB & "MB available on the C: Drive" & vbCrLf & _ "There is Currently: " & AvailSpaceGB & "GB available on the C: Drive" & vbCrLf FreeDiskSpace = AvailSpaceMB End Function
You need to login to post a comment.
