List of file names

I want to get a list of all the files (file paths to be precise) in a particular folder and all it’s sub-folders in a format that I can import into an Access database. How can I go about doing that?

Thanks
Grim

It would be nice to know what OS you’re on. If you’re on Windows 2000 or later (and probably earlier versions too), you can type dir /s > output.txt to a command prompt to get a file listing. You’ll have to do some cleaning.

I want to get a list of all the files (file paths to be precise) in a particular folder and all it’s sub-folders in a format that I can import into an Access database. How can I go about doing that?

Thanks
Grim

Windows XP - I’ll give that a try.

Grim

No Jokes!! Thanks - that gets me the raw data…

Grim

Late chiming in I know, but “dir /s /b >\whatever.txt” might be easier to work with.

Or you could install a third-party utility such as DirPrint

Thanks - but judicious use of Find and Replace and then importing as a fixed-length text file did the job without too much hassle. The file name proved to be sufficient, although yours actually gave me what I aksed for :slight_smile: .

Heh. Every once in a while, knowing good old DOS comes in handy for something. :slight_smile:

You can do it in DOS. Open the command (DOS) window in the directory of concern. Say you want to make a text file which is a list of the files in the directory. For the sake of illustration we will call it GrimFiles.txt

type DIR > GRIMFILES.TXT

the resulting is a text file which you can import into Windows programs.

You might prefer

DIR /S /B

This will give you the output in “bare” format, one filename per line. It will also recurse into subfolders as you requested. (the “/S” option)

Also works if you tack a “.xls” on the end, to an Excel file.

dir /s /b >filelist.xls

If you use dir /s /b > filelist.txt, you get a either a brand new file, or you’ll overwrite an existing file of that name. If you use dir /s /b >> filelist.txt, you get either a brand new file, or you’ll append the results to an existing file.

And you can filter the search parameters through use of various wildcards. You can also sort your output just about any way you want. Enter the command dir /? for help with this.

It might help to know what operating system you’re running. On Unix or Mac OSX systems, the command would be:

ls -R1

You can use the following sub as a starting point:



Public Sub FolderDir()
'stores the file names from FOLDER into
'the table TABLENAME, in the field FIELDNAME
Const FOLDER = "C:	emp\"   'seems to need trailing backslash
Const TABLENAME = "Table1"
Const FIELDNAME = "FileName"
Dim sFolderName As String, sFileName As String
Dim db As Database, rs As Recordset
sFileName = Dir(FOLDER)
If sFileName = "" Then
    MsgBox "Invalid Folder!"
Else
    Set db = CurrentDb
    Set rs = db.OpenRecordset(TABLENAME)
    Do Until sFileName = ""
        rs.AddNew
        rs(FIELDNAME) = sFileName
        rs.Update
        sFileName = Dir()
    Loop
    rs.Close
    Set rs = Nothing
    Set db = Nothing
End If
            
End Sub


Merged two threads.

-xash
General Questions Moderator