VB and VBA Users Source Code: Returning a file name without a path or file extension
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Returning a file name without a path or file extension
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, June 07, 2005
Hits:
2249
Category:
Files/Directories/IO
Article:
The code below returns a file name without the file path and file extension. 'Purpose : Returns a file name without a path or file extension. 'Inputs : sFileName The name of the file to return the base name of. 'Outputs : Returns a file name without a path or file extension. 'Author : Andrew Baker 'Date : 25/03/2001 'Notes : Function FileBaseName(sFileName As String) As String Dim lPosEnd As Long Dim lPosStart As Long 'Remove file extension lPosEnd = InStrRev(sFileName, ".") If lPosEnd = 0 Then lPosEnd = Len(sFileName) + 1 End If 'Find end of path lPosStart = InStrRev(sFileName, "\") If lPosStart = 0 Then lPosStart = InStrRev(sFileName, "/") End If 'Return file name with no path or extension FileBaseName = Mid$(sFileName, lPosStart + 1, lPosEnd - 1 - lPosStart) Exit Function ErrFailed: FileBaseName = "" Debug.Print "Error in FileBaseName: " & Err.Description Debug.Assert False End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder