Get File Information

Article Tools

System.IO.DirectoryInfoclass exposes instance methods for creating, moving, and enumerating through directories and subdirectories.

Public Function GetFileInfo( ByVal sourceFile, ByRef fileInfo )
   Dim srcInfo

   GetFileInfo = False
   Set srcInfo = DotNetFactory.CreateInstance( "System.IO.FileInfo",, sourceFile )
   Set fileInfo = CreateObject( "Scripting.Dictionary" )
    ' ** Determine whether the source file exists.
   If Not srcInfo.Exists Then
      Reporter.ReportEvent micWarning, "GetFileInfo", "Source file does not exists."
      Exit Function
   End If
   ' ** retrieving file information
   fileInfo.Add "CreationTime", srcInfo.CreationTime.ToString( "F" )
   fileInfo.Add "CreationTimeUtc", srcInfo.CreationTimeUtc.ToString( "F" )
   fileInfo.Add "LastAccessTime", srcInfo.LastAccessTime.ToString( "F" )
   fileInfo.Add "LastAccessTimeUtc", srcInfo.LastAccessTimeUtc.ToString( "F" )
   fileInfo.Add "LastWriteTime", srcInfo.LastWriteTime.ToString( "F" )
   fileInfo.Add "LastWriteTimeUtc", srcInfo.LastWriteTimeUtc.ToString( "F" )
   fileInfo.Add "Extension", srcInfo.Extension
   fileInfo.Add "FullName", srcInfo.FullName
   fileInfo.Add "IsReadOnly", srcInfo.IsReadOnly
   fileInfo.Add "Name", srcInfo.Name
   fileInfo.Add "DirectoryName", srcInfo.DirectoryName
   fileInfo.Add "Length", srcInfo.Length
   fileInfo.Add "Attributes", srcInfo.Attributes
   Set srcInfo = Nothing
   GetFileInfo = True

End Function

The Function receive two arguments. the first, a string that represents the full path of the file which we want to get the information.

the second argument is a reference variable, that will returned to the called thread as a Scripting.Dictionary.

The advantage for the dictionary reference argument is that the we get all the information, and the calling thread use the relevant one. so, is unnecessary to write a function to each property.

The function initialization sets the return value to false ( Failed ) and a variable of type System.IO.FileInfo is initialized to the sourceFile argument, using the class constructor. the same is done for the Scripting.Dictionary object.

GetFileInfo = False
Set srcInfo = DotNetFactory.CreateInstance( "System.IO.FileInfo",, sourceFile )
Set fileInfo = CreateObject( "Scripting.Dictionary" )

Than a verification is implemented, to avoid popup exceptions. if the file does not exists, a detailed message can be displayed in the Reporter object, and the function ends with the return value False

If Not srcInfo.Exists Then
   Reporter.ReportEvent micFail, "GetFileInfo", "Source file does not exists."
   Exit Sub
End If

After everything is initialized and verified, the function start to query the FileInfo class, and retrieve all the available information for the class.

As for the dates, the ToString method gives the flexibility of formatting the result. all the values added to the dictionary.

if the function reaches the and, the function will return True. this is how you can call the function:

Dim fileInfo
If GetFileInfo( "C:\LOG\InsertTransaction.log", fileInfo ) Then
    Print "CreationTime = " & fileInfo( "CreationTime" )
    Print "CreationTimeUtc = " & fileInfo( "CreationTimeUtc" )
    Print "LastAccessTime = " & fileInfo( "LastAccessTime" )
    Print "LastAccessTimeUtc = " & fileInfo( "LastAccessTimeUtc" )
    Print "LastWriteTime = " & fileInfo( "LastWriteTime" )
    Print "LastWriteTimeUtc = " & fileInfo( "LastWriteTimeUtc" )
    Print "Extension = " & fileInfo( "Extension" )
    Print "FullName = " & fileInfo( "FullName" )
    Print "Name = " & fileInfo( "Name" )
    Print "Parent = " & fileInfo( "Parent" )
    Print "IsReadOnly = " & fileInfo( "IsReadOnly" )
    Print "DirectoryName = " & fileInfo( "DirectoryName" )
    Print "Length = " & fileInfo( "Length" )
    Print "Attributes = " & fileInfo( "Attributes" )
End If

 

Previous postCreating a Text File Next postDelete all Temporary Files

Related Posts

Post Your Comment

You must be logged in to post a comment.