Get count of files with a particular file extension using WMI, .NET 2.0 using the CIM_Datafile class. 

by Steve Schofield

This article discusses my experience when trying to retrieve the number of files on a remote machine using the WMI CIM_DataFile class.   I found using WMI to count the number of files had issues and wouldn’t always return the results. It required the WMI service to be recycled to start working again. The script would just hang and not respond.   Another unique item is how WMI stores file location information.  For example, if you are looking for all files in the C:Inetpubmailrootqueue folder.   The ‘path’ has to be marked with two backslashes ‘\’ between each variable.  In this case the path used by the code would be formatted like \Inetpub\mailroot\queue\.

WMI is a powerful way to collect statistics but I found using the System.IO.DirectoryInfo class more reliable in this specific case.  The one drawback of using the System.IO.DirectoryInfo namespace I couldn’t figure out how to pass custom credentials like the WMI code sample listed below.  The credentials of the user id you are logged in as are used when using System.IO.DirectoryInfo.   The script ‘assumes’ whatever credentials at runtime.  If the code is in a console application and setup to run using Windows Task Scheduler, the user id and password that are setup on the scheduled task are used to authenticate.  In most cases this needs to be a domain admin or local administrator.  Hope this helps but it was a learning experience how WMI works under the covers.

Query with WMI and using the CIM_DataFile

Private Function GetQueueSize(ByVal strServerName As String, ByVal strPath As String, ByVal strUID As String, ByVal strPWD As String) As Integer
        Dim options As New ConnectionOptions()
        Dim retValue As New System.Text.StringBuilder
        options.Username = strUID
        options.Password = strPWD
        Dim scope As ManagementScope
        If strServerName = “LocalPCName” Then
            scope = New ManagementScope(“\” & strServerName & “rootcimv2”)
        Else
            scope = New ManagementScope(“\” & strServerName & “rootcimv2”, options)
        End If

        ‘Dim selectQuery As New SelectQuery(“SELECT * FROM CIM_DataFile Where PATH = ‘\inetpub\mailroot\queue\’ and Extension=’eml'”)
        ‘Dim selectQuery As New SelectQuery(“ASSOCIATORS OF {Win32_Directory.Name='” & strPath & “‘} Where ResultClass = CIM_DataFile”)
        Dim oSelectQuery As New SelectQuery(“SELECT * FROM CIM_DataFile where path='” & strPath & “‘ and Extension=’eml'”)
        Dim searcher As New ManagementObjectSearcher(scope, oSelectQuery)

        Try
            scope.Connect()
        Catch f As Exception
            Console.Write(f.Message.ToString())
        End Try

        Console.WriteLine(“ServerName : ” & strServerName & ” strPath ” & strPath & ” Number of files: ” & searcher.Get().Count)

    Return searcher.Get().Count()
End Function

Query with WMI and using System.IO.DirectoryInfo class

Sub GetDirectoryInfo(ByVal strServerName As String, ByVal path As String)
        ‘ Create a reference to the current directory.
        Dim di As New System.IO.DirectoryInfo(“\” & strServerName & path)

        ‘ Create an array representing the files in the current directory.
        Dim fi As System.IO.FileInfo() = di.GetFiles(“*.eml”)

        ‘ Print out the names of the files in the current directory.
        Dim x As Integer = UBound(fi)

        ‘writes out the # of files in the particular directory
         Console.writeline(x)

End Sub


     Powered by


 
 
copyright IISLogs 2009
Follow us on