VBA Get File Name with GetFileName (FSO)
This short tutorial will demonstrate how to use the GetFileName method of the FileSystemObject.
Get File Name with VBA FileSystemObject
This lesson uses the FileSystemObject. In order to use it, you will need to set a reference to the VB script run-time library.
For getting the file name from any path, you can use:
Sub FSOGetFileName()
Dim FileName As String
Dim FSO As New FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
'Get File Name
FileName = FSO.GetFileName("C:\ExamplePath\ExampleFile.txt")
'Get File Name no Extension
FileNameWOExt = Left(FileName, InStr(FileName, ".") - 1)
End Sub
FileName variable will then hold the value of “ExampleFile.txt”, FileNameWOExt variable will be without the extension “ExampleFile”.
Get File Name Without Extension
As noted above, to get the file name without extension use this line of code:
FileNameWOExt = Left(FileName, InStr(FileName, ".") - 1)