Visual Basic File API Routines
FindFirstFile: Extract Filename from a Full Path
     
Posted:   Sunday February 11, 2001
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows 95, Windows 2000
OS restrictions:   None
Author:   VBnet - Randy Birch
     

 

Related:  

FindFirstFile: Changing File and/or Folder Attributes Recursively
FindFirstFile: Fast Directory File Count

FindFirstFile: Extract Filename from a Full Path
FindFirstFile: Performance Comparison - FSO vs. API
FindFirstFile: Comparison of FindFirstFile and SearchTreeForFile
FindFirstFile: Save a Recursive Search of All Drives to Disk
FindFirstFile: Save a Recursive Search of Specified Drives to Disk
GetFileVersionInfo: File Search and File Property Info
GetLogicalDriveStrings: An API 'DriveExists' Routine
FindFirstFile: An API 'FileExists' Routine
FindFirstFile: An API 'FolderExists' Routine
     
 Prerequisites
None.

This function accepts a full path and filename for an existing file and returns just the filename portion. The string passed can represent either a long or short path format.
 BAS Module Code
None.

 Form Code
To the form, add the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const MAX_PATH As Long = 260
Private Const INVALID_HANDLE_VALUE As Long = -1

Private Type FILETIME
   dwLowDateTime As Long
   dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
   dwFileAttributes As Long
   ftCreationTime As FILETIME
   ftLastAccessTime As FILETIME
   ftLastWriteTime As FILETIME
   nFileSizeHigh As Long
   nFileSizeLow As Long
   dwReserved0 As Long
   dwReserved1 As Long
   cFileName As String * MAX_PATH
   cAlternate As String * 14
End Type
   
Private Declare Function FindFirstFile Lib "kernel32" _
   Alias "FindFirstFileA" _
  (ByVal lpFileName As String, _
   lpFindFileData As WIN32_FIND_DATA) As Long
   
Private Declare Function FindClose Lib "kernel32" _
  (ByVal hFindFile As Long) As Long
  
  
Private Sub Command1_Click()
  
   Print GetFileNameFromPath("c:\winnt\system32\msvbvm60.dll")
   
End Sub


Private Function GetFileNameFromPath(ByVal sFullPath As String) As String

   Dim WFD As WIN32_FIND_DATA
   Dim hFile As Long
         
   hFile = FindFirstFile(sFullPath, WFD)
   
   If hFile <> INVALID_HANDLE_VALUE Then
   
     'the filename portion is in cFileName
      GetFileNameFromPath = TrimNull(WFD.cFileName)
      Call FindClose(hFile)
      
   End If
   
End Function


Private Function TrimNull(startstr As String) As String

   Dim pos As Integer
   
   pos = InStr(startstr, Chr$(0))
   
   If pos Then
      TrimNull = Left$(startstr, pos - 1)
      Exit Function
   End If
  
   TrimNull = startstr
  
End Function
 Comments
Wildcards are not permitted in the passed string.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter