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\\.
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