DiskSpace.vbs


DiscSpace reports free / used disk space on partitions, folders and disks, and writes the result to a file. If run an regular intervals (as a scheduled task), it gives an idea of the data volume growth rate, which can be usefull to predict when a given partition or disk will be completely full, or to plan the necessary size of new disks


' collect size of folders / bytes used on partitions 
' result is written to log file with following format : 

'       date   partition0    partition1    folder0    folder1        
'       date   size(MB)      size(MB)      size(MB)   size(MB)        

' tabular text file => for import in spreadsheet 
' ====================================================================== 

On Error Resume Next 
Const ForAppending = 8 
Const LogFilePathName  = "C:\batch\DataSize.txt" 
TAB = chr(9) 
iDataSize = 0 

'logging volume (used) for this partition : 
        	Dim arrPartitions(1) 
arrPartitions(0) = "C"                               arrPartitions(1) = "D"                    

'logging volume for the following folders : 

       Dim arrFolders(5) 
       
       arrFolders(0) = "D:\DATA"
       arrFolders(2) = "d:\PL_Reports" 
       arrFolders(3) = "d:\home" 
       arrFolders(4) = "d:\Lotus\Notes\Data" 
       arrFolders(5) = "d:\Cubic" 
       arrFolders(6) = "e:\zfax" 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objLogFile = objFSO.OpenTextFile(LogFilePathName,ForAppending ,true,0) 

strLogEntry = date() 

'get volume (used) of partitions : 
        For i = Lbound (arrPartitions) to Ubound (arrPartitions) 
		set objDrive =objFSO.getDrive(arrPartitions(i)) 
		iDataSize = objDrive.TotalSize - objDrive.AvailableSpace 
		strLogEntry = strLogEntry & TAB & toMegaBytes (iDataSize) 
		iDatasize = 0 
       Next 

       Set objDrive = Nothing 

'get volume of the folders : 
        For i = Lbound (arrFolders) to Ubound (arrFolders) 

            If i > 0 Then 
		set objFolder =objFSO.getfolder(arrFolders(i)) 
		iDatasize = objFolder.Size 
		strLogEntry = strLogEntry & TAB & toMegaBytes (iDataSize) 
		iDatasize = 0 

		Else        	' workaround for "d:\data : path not found" 
				‘when trying to get size

			iDataSize = 0 
			set objFolder =objFSO.getfolder("d:\data") 
			set colSubFolders = objFolder.SubFolders 
			For each item in colSubFolders 
 				iDataSize = iDataSize +  item.Size 
			Next 
			strLogEntry = strLogEntry & TAB & toMegaBytes (iDataSize) 
			iDatasize = 0 
		End If 
        Next 

       Set objFolder = Nothing 


'log it 
       objLogFile.WriteLine(strLogEntry) 
       objLogFile.Close 


'clean up 
       Set objLogFile  = Nothing 
       Set objFSO = Nothing 
       Wscript.Quit 


'Functions 

Function toMegaBytes(bytes) 

                toMegaBytes = int ((bytes / 1024) /1024) 

End Function 

Function toGigaBytes(bytes) 

                toGigabytes = int (((bytes / 1024) /1024) / 1024) 

End Function 

' -------------------------------------------------------------------- 

'Alternative 
'get used space for partition 
'       For Each objDrive in colDrives 
'               If objDrive.DriveLetter = DataPartition Then 
'                       intDataSize = toMegaBytes ( objDrive.TotalSize - objDrive.AvailableSpace ) 
'               End If 
'       Next 
' 
'       Set colDrives = Nothing


		

Koen Noens
june 7, 2005