CodeItBetter Programming Another VB Programming Blog

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
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.