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

Related posts:

  1. How to Return the file portion of a file + pathname (in two ways)
  2. How to Return the Path portion of a file (in two ways)
  3. How to Get file extension for a given file (in two ways)
  4. How to Check file exists (in Six ways)
  5. How to get the file name without its extension and path
  6. How to return everything between ‘string2′ in ‘string1′
  7. How to Rename folder (in three ways)
  8. How to Move File from one directory to another (in three ways)
  9. How to delete the given file (in four ways)
  10. How to Rename files (in five ways)

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.