Written by Allen Wyatt (last updated April 20, 2019)
This tip applies to Excel 97, 2000, 2002, and 2003
Carol has a directory with about 1,000 files with names such as YR1905-LIC12345-Smith,Harry-Brown,Mary. She would like to bring all of these filenames (not the files themselves) into a worksheet and separate the names at the dash. Thus, the example filename would actually occupy four cells in a single row. Carol figures this will take a macro to accomplish, but she's not sure how to access the filenames in that macro.
You can, of course, use a macro to do this, but you don't need to use a macro. You can, instead, use an old DOS-era trick to get what you need. At the command prompt (accessible through Windows: Start | All Programs | Accessories | Command Prompt), navigate until you are in the directory that contains the files. Then enter the following:
dir /b /a-d > filelist.txt
This creates a text file (filelist.txt) that contains a list of all the files within the current directory. Now, within Excel, you can follow these steps:
Figure 1. The Text Import Wizard.
The above steps are fairly easy to accomplish, particularly if you only need to get the file listing into Excel once in a while. If you need to do it more routinely, then you should probably seek a way to do it using a macro. The following macro will work very quickly:
Sub GetFileNames() Dim sPath As String Dim sFile As String Dim iRow As Integer Dim iCol As Integer Dim splitFile As Variant 'specify directory to use - must end in "\" sPath = "C:\" iRow = 0 sFile = Dir(sPath) Do While sFile <> "" iRow = iRow + 1 splitFile = Split(sFile, "-") For iCol = 0 To UBound(splitFile) Sheet1.Cells(iRow, iCol + 1) = splitFile(iCol) Next iCol sFile = Dir ' Get next filename Loop End Sub
When you run the macro, make sure that there is nothing in the current worksheet. (Anything there will be overwritten.) Also, you should change the directory path that is assigned to the sPath variable near the beginning of the macro.
If you get an error when you run the macro, chances are good that you are using Excel 97. The Split function (used to break the filename apart at the dashes) was not added to VBA until Excel 2000. If you are using Excel 97, then you can use the following routine to emulate what the Split function does:
Function Split(Raw As String, Delim As String) As Variant Dim vAry() As String Dim sTemp As String Dim J As Integer Dim Indx As Integer Indx = 0 sTemp = Raw J = InStr(sTemp, Delim) While J > 0 Indx = Indx + 1 ReDim Preserve vAry(1 To Indx) vAry(Indx) = Trim(Left(sTemp, J)) sTemp = Trim(Mid(sTemp, J, Len(sTemp))) J = InStr(sTemp, Delim) Wend Indx = Indx + 1 ReDim Preserve vAry(1 To Indx) vAry(Indx) = Trim(sTemp) Split = vAry() End Function
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11143) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Pulling Filenames into a Worksheet.
Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!
Workbooks are loaded from disk files, but workbooks aren't the only type of files that Excel can load. This tip provides ...
Discover MoreWhen processing plain text files in a macro, it is often helpful to know how much data the file contains. The normal way ...
Discover MoreTracking down a problem that occurs with a single workbook can be vexing. One such problem occurred with Chris, and these ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2019-04-20 05:44:32
Hello,
I use an easier solution:
- Define the cell with name 'FileSearchPath' and value the files you want to search, e.g. C:\...\*.jpg;
P.S.: you can even get the current directory as follows: =CELL("filename");
- Define the name 'FileNameList' (you can use another name, but it doesn't make a difference) as follows:
- Formulas / Name Manager / New:
- Name: FileNameList;
- Ref: =FILES(FileSearchPath);
- In a 2nd worksheet you use in each row: =IFERROR(INDEX(FileNameList;ROW()-1);"") (I started in the 2nd row so that I can put a header in the 1st row.
- I've also a function 'GetField()' to get the fields in a structured string using a delimiter of your choice.
Kind regards,
Ignace Clarysse
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments