Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Pulling Filenames into a Worksheet.

Pulling Filenames into a Worksheet

by Allen Wyatt
(last updated April 26, 2016)

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:

  1. Within Excel, click on the Open tool on the toolbar. Excel displays the Open dialog box.
  2. Using the Files of Type drop-down list at the bottom of the dialog box, indicate that you want to open Text Files (*.prn; *.txt; *.csv).
  3. Navigate to and select the filelist.txt file you created at the command prompt.
  4. Click on Open. Excel starts the Text Import Wizard, displaying the Step 1 of 3 dialog box. (See Figure 1.)
  5. Figure 1. The Text Import Wizard.

  6. Make sure the Delimited choice is selected, then click on Next. Excel displays the Step 2 of 3 dialog box.
  7. Make sure you specify a dash as your delimiter. (You'll need to click on Other and then enter a dash as the delimiter.)
  8. Click on Finish. Your file is imported and broken at the dashes, just as you wanted.

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

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

The Case of the Vanished Menu Bar

Can't find your menu bar any more? Here are some things you can try to get that important feature back on your screen where ...

Discover More

Breaking Links in Lots of Documents

Breaking document links can be a tedious chore, especially if there are lots of links and lots of documents. This tip ...

Discover More

Extracting Street Numbers from an Address

Want to know how to move pieces of information contained in one cell into individual cells? This option exists in using ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

Appending to a Non-Excel Text File

Does your macro need to add information to the end of a text file? This is called appending, and is done using the technique ...

Discover More

Displaying Path Names in the Menu Bar

Want a quick way to see the full path name associated with a workbook? You can add a tool to the menu bar that displays the ...

Discover More

Checking for the Existence of a File

The data stored in a worksheet can often correspond to information external to that worksheet. For instance, you might ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is eight more than 8?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.