How to Return the file portion of a file + pathname without extension (in two ways)
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 'File/Folder Handling - How to Return the file portion of a file + pathname without extension (in two ways) Option Explicit 'Non FSO Method: 'Method 1: Public Function GetFileWithExtensionUsingNonFSO(ByVal sFileName As String) As String sFileName = Mid$(sFileName, InStrRev(sFileName, "\") + 1) GetFileWithExtensionUsingNonFSO = Left$(sFileName, InStrRev(sFileName, ".") - 1) End Function 'Method 2: Public Function GetFileWithExtensionUsingNonFSO1(ByVal sFileName As String) As String Dim iStartPos As Integer, iEndPos As Integer iStartPos = InStrRev(sFileName, "\") + 1 iEndPos = InStrRev(sFileName, ".") GetFileWithExtensionUsingNonFSO1 = Mid$(sFileName, iStartPos, iEndPos - iStartPos) End Function 'FSO Method: Public Function GetFileWithExtensionUsingFSO(ByVal sFileName As String) As String 'Set a reference to "Microsoft Scripting Runtime" Dim FSO As Scripting.FileSystemObject Set FSO = New Scripting.FileSystemObject GetFileWithExtensionUsingFSO = FSO.GetBaseName(sFileName) Set FSO = Nothing End Function |
Related posts:
- How to Return the file portion of a file + pathname (in two ways)
- How to Return the Path portion of a file (in two ways)
- How to Get file extension for a given file (in two ways)
- How to Check file exists (in Six ways)
- How to get the file name without its extension and path
- How to return everything between ‘string2′ in ‘string1′
- How to Rename folder (in three ways)
- How to Move File from one directory to another (in three ways)
- How to delete the given file (in four ways)
- How to Rename files (in five ways)