Return to VBA Code Examples

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)

 

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! vba save as


Learn More!