Excel VBA List Files in Folder Without Extension
In this article
To list files in a folder without their file extensions using Excel VBA, you can use the Dir function or the FileSystemObject and then strip the extension from each filename with the RemoveExtension method or by using the Split function on the dot. The simplest approach is to loop through files with Dir and use CreateObject("Scripting.FileSystemObject").GetBaseName to get the file name without the extension.
Background: Why You Might Need This
When automating file management in Excel, you often need to generate a list of files for reporting, batch processing, or data import. By default, VBA's Dir function returns the full filename including the extension (e.g., "report.xlsx"). However, many tasks require only the base name—for example, when matching files to a list of expected names, creating hyperlinks, or generating file inventories where the extension is irrelevant. Removing the extension ensures consistency, especially when files of different types (e.g., .xlsx, .csv, .txt) share the same base name.
How It Works: The Core Methods
There are three common ways to list files without extensions in VBA:
- Using
DirandInStrRev: Loop through files withDir, then useLeft(filename, InStrRev(filename, ".") - 1)to remove the extension. This works for files with a single dot, but fails if the filename contains multiple dots (e.g., "my.file.txt" becomes "my"). - Using
FileSystemObject(FSO): Create a reference toScripting.FileSystemObject(or late bind withCreateObject), iterate through theFolder.Filescollection, and use theGetBaseNamemethod. This is the most reliable and handles multiple dots correctly. - Using
Splitfunction:Split(filename, ".")(0)gives the part before the first dot, but again fails for names with multiple dots.
The FSO method is recommended for robustness and clarity.
Step-by-Step: Using FileSystemObject
Here is a complete VBA subroutine that lists all files in a specified folder (without extensions) into column A of the active worksheet:
- Press
Alt+F11to open the VBA editor. - Insert a new module (Insert > Module).
- Paste the following code:
Sub ListFilesWithoutExtension()
Dim fso As Object
Dim folder As Object
Dim file As Object
Dim i As Long
Dim folderPath As String
' Change this to your target folder
folderPath = "C:\YourFolder\"
' Ensure folder path ends with backslash
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
i = 1
For Each file In folder.Files
Cells(i, 1).Value = fso.GetBaseName(file.Name)
i = i + 1
Next file
Set file = Nothing
Set folder = Nothing
Set fso = Nothing
End SubThis code lists all files in the folder, ignoring subfolders. To include subfolders, you would need a recursive procedure.
How to Choose: Dir vs. FileSystemObject
The Dir function is lightweight and does not require external references, making it ideal for simple scripts. However, it has limitations: it only returns the first file matching a pattern unless you call it repeatedly with no arguments, and it does not provide a direct method to get the base name. The FileSystemObject is more powerful, offering properties like DateCreated, Size, and Type, and its GetBaseName method is exactly what you need. The trade-off is that FSO requires a reference to the Microsoft Scripting Runtime (or late binding via CreateObject) and is slightly slower for very large folders. For most tasks, FSO is the better choice.
Common Problems and Solutions
- Files with multiple dots: Using
InStrRevorSplitwith the first dot will truncate incorrectly. Solution: UseGetBaseName. - Hidden files: By default,
Dirand FSO do not list hidden files unless you set theShowHiddenattribute. In FSO, setfolder.Filesincludes hidden files by default? Actually, FSO includes hidden files, butDirdoes not. If you need to exclude hidden files, you can check theAttributesproperty. - Empty folder: The loop simply does nothing, which is fine.
- File names with no extension:
GetBaseNamereturns the full name if there is no dot, which is correct.
Comparisons: BaseName vs. Manual Stripping
Let's compare the three methods with a filename like "project.report.final.xlsx":
GetBaseNamereturns "project.report.final" — correct.Left(name, InStrRev(name, ".") - 1)returns "project.report.final" — correct because it finds the last dot.Split(name, ".")(0)returns "project" — incorrect.
So InStrRev is actually a viable alternative to FSO, but it fails if the file has no extension (it would return the whole name, which is fine) or if the file name is just ".hidden" (it would return an empty string). GetBaseName handles these edge cases more gracefully.
Safety and Accuracy Considerations
When writing VBA code that interacts with the file system, always ensure the folder path exists to avoid runtime errors. Use Dir(folderPath, vbDirectory) to check existence before proceeding. Also, be careful with file system permissions—if the folder is protected, the code may throw an error. Always test on a sample folder first. Additionally, if you are writing results to a worksheet, clear the target range beforehand to avoid leftover data.
Frequently Asked Questions
Can I list files without extension using the Dir function?
Yes, but you need to manually remove the extension. Use Dir to get each filename, then apply Left(filename, InStrRev(filename, ".") - 1) to strip the extension. This works for most cases, but be aware of files with multiple dots—it still works because InStrRev finds the last dot.
How do I include subfolders in the list?
You need a recursive procedure that calls itself for each subfolder. With FSO, you can loop through folder.SubFolders and call the same routine for each subfolder. Alternatively, use Dir with the vbDirectory attribute to traverse subfolders manually.
What if I want to list only certain file types, like .xlsx?
You can filter by extension before stripping it. For example, with FSO, check fso.GetExtensionName(file.Name) = "xlsx" before adding the base name to your list. With Dir, use a pattern like "*.xlsx" to only retrieve those files.
Does GetBaseName work for files without any extension?
Yes. If the file has no dot, GetBaseName returns the entire filename. This is the desired behavior because there is no extension to remove.
How can I avoid errors when the folder path is invalid?
Use Dir(folderPath, vbDirectory) to check if the folder exists before proceeding. If it returns an empty string, display a message and exit the subroutine. This prevents runtime errors.