Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Pulling Filenames into a Worksheet.

Pulling Filenames into a Worksheet

Written by Allen Wyatt (last updated October 30, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


3

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. To access the command prompt if you are using a later version of Excel, simply type "command prompt" (without the quotes) in the search box next to the Windows icon in the task bar. If you are using an older version of Excel, the command prompt is accessible through Windows: Start | All Programs | Accessories | Command Prompt. At the command prompt navigate until you are in the directory that contains the files. Let's assume, for this example, that you are trying to get a listing of the files in this directory:

c:\Users\e07482\My Documents\rnp

To navigate to that directory, enter in this command at the command prompt:

chdir "\Users\e07482\My Documents\rnp"

You need to use the quote marks around the directory because of the space in the path name. Then, type the following command to see, on-screen, what the file listing is:

dir /b /a-d

The "/a-d" part means "don't list directories." If you are satisfied with what you see on the screen, then you can send it to the file by using 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. Display the Open dialog box. (In Excel 2007, click the Office button and then click Open. In Excel 2010, click the File tab of the ribbon and then click Open. In Excel 2013, click the File tab of the ribbon, click Open, then Computer, and finally Browse. In later versions of Excel, click the File tab of the ribbon and click Browse.)
  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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11144) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel 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

Creating Custom Document Properties

Word allows you to keep track of any number of custom properties about a document. Here's how to create those properties ...

Discover More

Progression Indicator in a Macro

When your macro is humming along, minding its own business, a user watching the screen may not see any activity and ...

Discover More

Printing a List of Custom Styles

You can add any number of styles to your document in order to define how you want your text to appear. If you later want ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Full Path Names in Excel

Need to know what the full path name is for the current workbook? With a simple macro you can display the full path name ...

Discover More

Loading Unwanted Files at Startup

Imagine how painful it would be if every time you started Excel it tried to load all the files in your root directory? ...

Discover More

Full Path to Workbook

Need to get the full path of your current workbook into the Clipboard? Excel doesn't make it quite as easy as it used to ...

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}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. 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 two more than 7?

2021-11-02 02:25:24

Philip

For the Mac users : in MacOS, in any Finder window listing files and/or folders, just "select all" (command+A), press command+C, then go into Excel and press command+V and the entire list of file/folder names of the selected items in the Finder window will be pasted in the worksheet.


2021-10-31 04:54:08

Mike

If one is going to go the command prompt way, it's much easier first to navigate to the correct directory in windows explorer, then type
cmd
in the main top bar (not the search bar); this will open a command prompt window in the desired directory.

Then type
dir /b /a-d | clip
and the file list will be sent to the clipboard. Just paste into excel.


2021-10-30 10:04:07

J. Woolley

My Excel Toolbox includes this dynamic array function:
ListFiles([RootFolder],[SkipSubfolders],[SkipHidden],[SkipHeader])
In older versions of Excel you can use it with the SpillArray function like this:
=SpillArray(ListFiles([RootFolder],[SkipSubfolders],...))
See https://sites.google.com/view/MyExcelToolbox/


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu 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.