Professional Windows hosting from ORCS Web
    
An Unrivaled Windows Hosting Experience    
1-888-313-9421  |  webteam@orcsweb.com        
From Our Clients:
"This reinforces why you are #1 in service and support. There are millions of us non-techie people who appreciate the service."

Join our community of clients at: 1-888-313-9421

Get Count of Files With a Particular File Extension Using WMI, .NET 2.0 Using the CIM_Datafile Class
By Steve Schofield
April 26, 2006

This article discusses my experience when trying to retrieve the number of files on a remote server using the WMI CIM_DataFile class. I found that using WMI to count the number of files had issues and would not 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 information. For example, if you are looking for all files in the C:\Inetpub\mailroot\queue folder. The 'path' has to be marked with two backslashes '\\' between each variable. In this case, WIM stores the path 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 is that I could not figure out how to pass custom credentials like WMI. When using System.IO.DirectoryInfo namespace, the credentials at runtime are used.  In most cases this needs to be a domain admin or local administrator.

Hope this helps.  It was a learning experience for me how WMI works under the covers. 

Module Module1

 

    Sub Main()

        Dim intX As Integer

        Dim intZ As Integer

 

        'Uses CIM_DataFile class to return number of files

        intX = GetQueueSize( _

            "127.0.0.1", _

            "\\inetpub\\mailroot\\drop\\", _

            Nothing, _

            Nothing)

        Console.WriteLine(intX)

 

        'Uses System.IO.DirectoryInfo namespace to return number of files

        intZ = GetDirectoryInfo("127.0.0.1", "c:\inetpub\mailroot\drop")

        Console.WriteLine(intX)

    End Sub

 

    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 System.Management.ConnectionOptions()

        Dim retValue As New System.Text.StringBuilder

        options.Username = strUID

        options.Password = strPWD

        Dim scope As System.Management.ManagementScope

        If strServerName = "127.0.0.1" Then

            scope = New System.Management.ManagementScope( _

                "\\" & strServerName & "\root\cimv2")

        Else

            scope = New System.Management.ManagementScope( _

                "\\" & strServerName & "\root\cimv2", _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 System.Management.SelectQuery( _

            "SELECT * FROM CIM_DataFile where path='" & strPath & _

            "' and Extension='eml'")

        Dim searcher As New System.Management.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

 

 

    Function GetDirectoryInfo( _

            ByVal strServerName As String, _

            ByVal path As String) As Integer

        'Create a reference to the current direcory.

        Dim di As System.IO.DirectoryInfo

 

        If strServerName = "127.0.0.1" Then

            di = New System.IO.DirectoryInfo(path)

        Else

            di = New System.IO.DirectoryInfo("\\" & strServerName & path)

        End If

 

 

        '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

        Return x

    End Function

End Module
Steve Schofield is a Senior Internet Support Specialist with ORCS Web, Inc. - a company that provides managed hosting solutions for clients who develop and deploy their applications on Microsoft Windows platforms. Services include shared hosting, dedicated hosting, and webfarm hosting, with specialty in .Net, SQL Server, and architecting highly scalable solutions.

Copyright © 1996-2010 ORCS Web, Inc. All rights reserved.