Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Find the HDD space everyday

Hi,

I want to find the HDD space .In my file server the space keeps on changing so need a scrip to keep a track of the space every day.
ex:
Todays Date
Todays date  : D Drive :  1.5 GB
Todays date  : E Drive :  1.5 GB

Tommorows date
Tommorows date : D Drive :  1.9 GB
Tommorows date : E Drive :  1.7 GB

regards
Sharath
Avatar of Speshalyst
Speshalyst
Flag of India image

Sharath,

there is a similar thread on EE ... which talks about a script which checks the drive space before running a different command..

you could use the same script.. but just modify it as u might deem fit..

https://www.experts-exchange.com/questions/21129294/determine-TOTAL-disk-size-in-DOS.html?qid=21129294
Hi Sharath,

Here is a script I use to check hard drive space, but it does not keep a log as yet.  It could easily be changed to do that though, if you would like that.
'============

Option Explicit

Dim strComputer, objWMIService, colDisks, objDisk, intFreeSpace, intTotalSpace, pctFreeSpace, strDetails

strComputer = InputBox("Please enter the computer that you wish to get the free space from:", "Free Space", "NTFP")

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk Where DriveType = 3")
For Each objDisk in colDisks
    intFreeSpace = objDisk.FreeSpace
    intTotalSpace = objDisk.Size
    pctFreeSpace = intFreeSpace / intTotalSpace
    strDetails = strDetails & objDisk.DeviceID & " - Total: " & Round(intTotalSpace / (1024 * 1024 * 1024), 2) & " - Free: " & Round(intFreeSpace / (1024 * 1024 * 1024), 2) & " GB - " & FormatPercent(pctFreeSpace) & " free" & vbCrLf
Next

MsgBox strDetails
'================

Regards,

Rob.
Avatar of bsharath

ASKER

Yes rob i want it to have a log maintained on every hr or 2 hrs basis.
Actually what i want is.To keep a record on todays space and then run every day to check the difference
Hi Sharath,

Please try this script:
'====================
Option Explicit

Dim strComputer, objWMIService, colDisks, objDisk, intFreeSpace, intTotalSpace, pctFreeSpace, strDetails
Dim strLogFileName, objFSO, objLogFile, boolDataExists, strLastLine
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const intForAppending = 8

strLogFileName = "HardDiskSpaceLog.csv"

If WScript.Arguments.Count < 1 Then
      strComputer = InputBox("Please enter the computer that you wish to get the free space from:", "Free Space", "NTFP")
Else
      strComputer = WScript.Arguments(0)
End If

If objFSO.FileExists(strLogFileName) = False Then
      strDetails = "Date,Computer,C Total (GB),C Free,C % Free,D Total (GB),D Free,D % Free,E Total (GB),E Free,E % Free,C Diff,D Diff,E Diff" & VbCrLf
      boolDataExists = False
Else
      strDetails = VbCrLf
      boolDataExists = True
End If

strDetails = strDetails & """" & Now & """," & strComputer

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk Where DriveType = 3")

For Each objDisk in colDisks
    intFreeSpace = objDisk.FreeSpace
    intTotalSpace = objDisk.Size
    pctFreeSpace = intFreeSpace / intTotalSpace
    strDetails = strDetails & "," & Round(intTotalSpace / (1024 * 1024 * 1024), 2) & "," & Round(intFreeSpace / (1024 * 1024 * 1024), 2) & "," & FormatPercent(pctFreeSpace)
Next

Set objDisk = Nothing
Set colDisks = Nothing
Set objWMIService = Nothing

If boolDataExists = True Then
      Set objLogFile = objFSO.OpenTextFile(strLogFileName, intForReading, False)
      While Not objLogFile.AtEndOfStream
            strLastLine = objLogFile.ReadLine
      Wend
      objLogFile.Close
      Set objLogFile = Nothing
      strDetails = strDetails & "," & Split(strLastLine, ",")(2) - Split(strDetails, ",")(2)
      strDetails = strDetails & "," & Split(strLastLine, ",")(5) - Split(strDetails, ",")(5)
      strDetails = strDetails & "," & Split(strLastLine, ",")(8) - Split(strDetails, ",")(8)
End If

Set objLogFile = objFSO.OpenTextFile(strLogFileName, intForAppending, True)
objLogFile.Write strDetails
objLogFile.Close
Set objLogFile = Nothing
Set objFSO = Nothing

MsgBox "Done"
'=====================

It will output to a CSV file, which you can change the name of with
strLogFileName = "HardDiskSpaceLog.csv"

This work under the assumption that there are three physical drives located on the server (C, D, and E).
If your server is different, let me know, and I will change the field references for you.

It records the difference in GB from when you run it, to that value from the previous time it was run.

Regards,

Rob.
I get this.

Date      Computer      C Total (GB)      C Free      C % Free      D Total (GB)      D Free      D % Free      E Total (GB)      E Free      E % Free      C Diff      D Diff      E Diff
7/27/2007 8:24      dev-chen-mrd100      24.41      15.84      64.89%      124.63      91.1      73.10%      39.07      12.11      31.01%      35.49      6.61      18.64%

What does C diff mean.

I have different machines with different Partitions in this case it is C,D,E,F.Is there a way to let the script search till F.If there is a machine which has until D then search without any errors.
Need to allow the script to search C,D,E,F,G,H,I drives
OK, I've tried to make it completely automatic, to pick up all Physical drives.....try this:
'==================
Option Explicit

Dim strComputer, objWMIService, colDisks, objDisk, intFreeSpace, intTotalSpace, pctFreeSpace, strDetails
Dim strLogFileName, objFSO, objLogFile, boolDataExists, strLastLine, intCount, intDriveCount
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const intForAppending = 8

If WScript.Arguments.Count < 1 Then
      strComputer = InputBox("Please enter the computer that you wish to get the free space from:", "Free Space", "NTFP")
Else
      strComputer = WScript.Arguments(0)
End If

strLogFileName = strComputer & "_HardDiskSpaceLog.csv"

strDetails = strDetails & """" & Now & """," & strComputer

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk Where DriveType = 3")

intDriveCount = 0
For Each objDisk in ColDisks
      intDriveCount = intDriveCount + 1
Next

If objFSO.FileExists(strLogFileName) = False Then
      strDetails = "Date,Computer"
      boolDataExists = False
      For intCount = 1 To intDriveCount
            strDetails = strDetails & "," & Chr(intcount + 66) & " Total (GB)," & Chr(intcount + 66) & " Free," & Chr(intcount + 66) & " % Free"
            '",D Total (GB),D Free,D % Free,E Total (GB),E Free,E % Free"
      Next
      For intCount = 1 To intDriveCount
            strDetails = strDetails & "," & Chr(intcount + 66) & " Diff"
            '",D Diff,E Diff"
      Next
      strDetails = strDetails & VbCrLf
Else
      strDetails = VbCrLf
      boolDataExists = True
End If

strDetails = strDetails & """" & Now & """," & strComputer

For Each objDisk in colDisks
    intFreeSpace = objDisk.FreeSpace
    intTotalSpace = objDisk.Size
    pctFreeSpace = intFreeSpace / intTotalSpace
    strDetails = strDetails & "," & Round(intTotalSpace / (1024 * 1024 * 1024), 2) & "," & Round(intFreeSpace / (1024 * 1024 * 1024), 2) & "," & FormatPercent(pctFreeSpace)
Next

Set objDisk = Nothing
Set colDisks = Nothing
Set objWMIService = Nothing

If boolDataExists = True Then
      Set objLogFile = objFSO.OpenTextFile(strLogFileName, intForReading, False)
      While Not objLogFile.AtEndOfStream
            strLastLine = objLogFile.ReadLine
      Wend
      objLogFile.Close
      Set objLogFile = Nothing
      strDetails = strDetails & "," & Split(strLastLine, ",")(2) - Split(strDetails, ",")(2)
      strDetails = strDetails & "," & Split(strLastLine, ",")(5) - Split(strDetails, ",")(5)
      strDetails = strDetails & "," & Split(strLastLine, ",")(8) - Split(strDetails, ",")(8)
End If

Set objLogFile = objFSO.OpenTextFile(strLogFileName, intForAppending, True)
objLogFile.Write strDetails
objLogFile.Close
Set objLogFile = Nothing
Set objFSO = Nothing

MsgBox "Done"
'===================

Regards,

Rob.
Oh doh!  I forgot one bit.

Change these three lines:
      strDetails = strDetails & "," & Split(strLastLine, ",")(2) - Split(strDetails, ",")(2)
      strDetails = strDetails & "," & Split(strLastLine, ",")(5) - Split(strDetails, ",")(5)
      strDetails = strDetails & "," & Split(strLastLine, ",")(8) - Split(strDetails, ",")(8)

To this:
      For intCount = 1 To intDriveCount
            If intCount = 1 Then
                  strDetails = strDetails & "," & Split(strLastLine, ",")(2) - Split(strDetails, ",")(2)
            Else
                  strDetails = strDetails & "," & Split(strLastLine, ",")(2 + (intCount - 1) * 3) - Split(strDetails, ",")(2 + (intCount - 1) * 3)
            End If
      Next

And it will calculate the right differences.

Oh, and run it two or three times, you'll see that the C Diff, D Diff columns, etc, will be zero, because the values haven't changed.

Regards,

Rob.
But now its creating new file for each search.I want a single file.
Now its not showing the C difference
Yeah, I made it make a new file per server, because the amount of drives are going to be different per server, so they will need to keep separate log files.
I should continue to append to that server's log file, if it already exists.

Rob.
Ok thanks .

One small problem

I ran the script on a machine 3 times and made some changes on the drives.

Date      Computer      C Total (GB)      C Free      C % Free      D Total (GB)      D Free      D % Free      E Total (GB)      E Free      E % Free      F Total (GB)      F Free      F % Free      C Diff      D Diff      E Diff      F Diff
7/27/2007 9:00      dev-chen-mrd100      24.41      15.84      64.87%      124.63      91.1      73.10%      39.07      12.11      31.01%      35.49      6.53      18.40%                        
7/27/2007 9:00      dev-chen-mrd100      24.41      15.84      64.87%      124.63      91.1      73.10%      39.07      12.11      31.01%      35.49      6.53      18.40%      0      0      0      0
7/27/2007 9:03      dev-chen-mrd100      24.41      16.34      66.91%      124.63      90.6      72.70%      39.07      12.11      31.01%      35.49      6.53      18.40%      0      0      0      0

But i still see the Difference as 0
Doh!  The calculation is happening from the Total, which will never change!  Stupid me!

I'll change it.....
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia 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 a lot..
No problem.  I like that actually.  I originally had it only outputting the values to screen, but this logging has potential for good historical data.

Rob.