Module: APIFilesAndPaths in Category Windows : File and Disk Operations from Total Visual SourceBook

Working with drives, paths, and files in VB6 and VBA with 32 and 64-bit Windows API calls.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the modAPIFilesAndPaths module.
ConvertPathToUNC Procedure Convert the named path to its universal naming convention (UNC) representation. This function returns the full UNC (Universal Naming Convention) path for the given file system path. For example, assume that your F: drive is mapped to a shared network drive on your server named "dev". The UNC mapping for this would be \\dev\directoryname where directoryname is the name of the directory the drive is mapped to. If you specify a path and a file name and extension for the strFileName argument, the file name and extension are returned along with the UNC path. If the path you pass is not a mapped network drive, this procedure calls the Windows API to get your computer name. This name becomes the root of the UNC path. For example, if your computer is named "MyComp", and your C drive has the label "MyC", the UNC path would be \\MyComp\MyC.
DriveFromPath Procedure Get the drive letter part of the path.
FilesToArrayAPI Procedure Populate the passed array with a list of files in the specified directory. This search is extremely fast compared to other searches. It is considerably faster than using the Windows Search dialog as well.
GetFileHandle Procedure Open a file to get a handle to it. We then use this handle for other calls. This function may fail if the file is already opened.
GetFileSize Procedure Get the size of the specified file in bytes.
GetFileTime Procedure Get the specified file time for the specified file. To specify the type of time you want returned, use the appropriate EnumFileTimeType value.
GetFileVersion Procedure Get the version information from a file such as a DLL, EXE, OCX, etc.
GetLongFileName Procedure Get the full path and name of a file in long file name format, given a path in DOS compatible short file name format.
GetShortFileName Procedure Get the DOS-compatible short file name version of a long file name.
NullTerminatedStringToArray Procedure Convert a null-terminated string to the passed array.
PathFromFullPath Procedure Get the remainder of a path after the drive letter.
SetFileTime Procedure Set the specified time on the specified file.
SubDirsToArrayAPI Procedure Populate the passed array with a list of subdirectories in the specified directory.
SystemTimeToVBATime Procedure Convert the Windows SYSTEMTIME structure to a VBA Date type.
VBATimeToFileTime Procedure Convert the passed date value to the Windows FILETIME structure. When calling the Windows API to work with file times, you must convert to and from VBA Date type serial values and the appropriate Windows time structure. This procedure converts a VBA Date type serial date to the Windows SYSTEMTIME structure.
VBATimeToSystemTime Procedure Convert the supplied VBA date/time value to the Windows SYSTEMTIME struct.
WinTimeToVBATime Procedure Convert the Windows FILETIME structure to a VBA Date type. When calling the Windows API to work with file times, you must convert to and from VBA Date type serial values and the appropriate Windows time structure. This procedure converts a Windows FILETIME structure to a VBA-compatible Date type serial date.
' Example of the modAPIFilesAndPaths module
'
' To use this example:
' 1. Create a new form.
' 2. Create five command buttons called:
'     - cmdGetFileTime
'     - cmdSetFileTime
'     - cmdLongShortName
'     - cmdPathToUNC
'     - cmdFindFiles
' 3. Paste the entire contents of this module into the
'    new form's module.
'
' This example assumes that the sample files are located in the folder named by the following constant.
Private Const mcstrSamplePath As String = "C:\Total Visual SourceBook 2013\Samples\"

Private Sub cmdGetFileTime_Click()
  Dim strFile As String
  Dim strTmp As String

  strFile = mcstrSamplePath & "sample.mdb"

  ' Build up information about the file into a string
  strTmp = "File: " & strFile & vbCrLf & _
           "Size: " & GetFileSize(strFile) & vbCrLf & _
           "Date Accessed: " & GetFileTime(strFile, ftt_Accessed, True) & vbCrLf & _
           "Date Modified: " & GetFileTime(strFile, ftt_Modified, True) & vbCrLf & _
           "Date Created: " & GetFileTime(strFile, ftt_Created, True) & vbCrLf & _
           "File Version: " & GetFileVersion(strFile)

  ' Display the information
  Debug.Print strTmp

End Sub

Private Sub cmdSetFileTime_Click()
  Dim strFile As String
  Dim strTmp As String

  ' Now change the time of a file
  strFile = mcstrSamplePath & "TESTTIME.TXT"

  ' Build up a string with information about dates and times
  strTmp = "Date Access for " & strFile & " is " & vbCrLf & Format$( _
           GetFileTime(strFile, ftt_Accessed, True), "MM/dd/yyyy hh:mm:ss") & vbCrLf

  ' Change the time
  SetFileTime strFile, Now, ftt_Modified

  ' Show the details after the change
  strTmp = strTmp & "After changing to Now(), Date Access is " & vbCrLf & _
           Format$(GetFileTime(strFile, ftt_Accessed, True), "MM/dd/yyyy hh:mm:ss")

  ' Display the results
  Debug.Print strTmp

End Sub

Private Sub cmdLongShortName_Click()
  Dim strFile As String
  Dim strTmp As String

  strFile = mcstrSamplePath & "sample.mdb"

  ' Build up information about long and short names
  strTmp = "Long File Name: " & GetLongFileName(strFile) & vbCrLf & _
           "Short File Name: " & GetShortFileName(strFile)

  ' Display the results
  Debug.Print strTmp

End Sub

Private Sub cmdPathToUNC_Click()
  Dim strFile As String
  Dim strTmp As String

  strFile = mcstrSamplePath & "sample.mdb"

  ' Make the call the get the UNC path
  strTmp = "UNC Path for: " & strFile & " is: " & vbCrLf & _
           ConvertPathToUNC(strFile)

  ' Display the results
  Debug.Print strTmp

End Sub

Private Sub cmdFindFiles_Click()

  Dim arrSubDirsFound() As String
  Dim arrLocationsFound() As String
  Dim intCounter As Integer

  intCounter = SubDirsToArrayAPI(mcstrSamplePath, arrSubDirsFound(), True, True, True, True)
  Debug.Print intCounter
  For intCounter = 0 To UBound(arrSubDirsFound)
    Debug.Print intCounter & ": " & arrSubDirsFound(intCounter)
  Next intCounter

  ReDim arrLocationsFound(0)
  intCounter = FilesToArrayAPI(mcstrSamplePath, "*.txt", 0, True, arrLocationsFound(), True, True, True, True)
  Debug.Print intCounter
  For intCounter = 0 To UBound(arrLocationsFound)
    Debug.Print intCounter & ": " & arrLocationsFound(intCounter)
  Next intCounter

End Sub

Private Sub Form_Load()
  ' Setup controls

  With Me.cmdGetFileTime
    .Top = 100
    .Left = 100
    .Width = 5000
    .Height = 400
    .Caption = "Get File Info"
  End With

  With Me.cmdSetFileTime
    .Top = 600
    .Left = 100
    .Width = 5000
    .Height = 400
    .Caption = "Change File Time"
  End With

  With Me.cmdLongShortName
    .Top = 1100
    .Left = 100
    .Width = 5000
    .Height = 400
    .Caption = "Get File Long && Short Name"
  End With

  With Me.cmdPathToUNC
    .Top = 1600
    .Left = 100
    .Width = 5000
    .Height = 400
    .Caption = "Get File UNC Path"
  End With

  With Me.cmdFindFiles
    .Top = 2100
    .Left = 100
    .Width = 5000
    .Height = 400
    .Caption = "Find Files"
  End With

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS