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. Let’s assume, for this example, that you are trying to get a listing of the files in this directory:
To navigate to that directory, enter in this command at the command prompt:
chdir "Userse07482My Documentsrnp"
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:
- Display the Excel Options dialog box. (In Excel 2007 click the Office button and then click Excel Options. In Excel 2010 and Excel 2013 display the File tab of the ribbon and then click Options.)
- 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).
- Navigate to and select the filelist.txt file you created at the command prompt.
- Click on Open. Excel starts the Text Import Wizard, displaying the Step 1 of 3 dialog box. (See Figure 1.)
- Make sure the Delimited choice is selected, then click on Next. Excel displays the Step 2 of 3 dialog box.
- Make sure you specify a dash as your delimiter. (You’ll need to click on Other and then enter a dash as the delimiter.)
- Click on Finish. Your file is imported and broken at the dashes, just as you wanted.
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.