Get file name

Good Afternoon!
I have lot of files in a folder which I need to transfer their names to a TXT or Excel file. How do I do it without coping their names manually (one by one)?
I appreciate your help.

best regards,
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Look at the Dir Function in VB help
Somethis like this is what I had in mind

Private Sub Form_Load()
    MsgBox ListFiles("C:\")
End Sub

Public Function ListFiles(ByVal strPath As String) As String
    Dim strWork As String
    Dim strReturn As String
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
    strWork = Dir(strPath, vbNormal + vbDirectory)
    Do While strWork <> ""   ' Start the loop.
        ' Ignore the current directory and the encompassing directory.
        If strWork <> "." And strWork <> ".." Then
            ' Use bitwise comparison to make sure MyName is a directory.
            If (GetAttr(strPath & strWork) And vbDirectory) = vbDirectory Then
                strReturn = strReturn & "[" & strWork & "]" & vbCrLf
                strReturn = strReturn & strWork & vbCrLf
            End If
        End If
        strWork = Dir   ' Get next entry.
    ListFiles = strReturn
End Function
You could save the returned string to a file or work with it as you need.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

If this is a one shot, you could also just use a DOS command like this to get a listing into a text file

dir /b > c:\list.txt

(This works on Win2000, if you have problems with the /b on your system, remove it.)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
If you just need a text file, you can use this command at a DOS prompt:


The the DIRCONTENTS.TXT file will contain your list. The /B parameter means 'bare' so it dumps the file names and nothing else.

For doing it in VB, look into the FileSystemObject. It allows you to open a folder and then loop through its contents:

    Dim FSO As New FileSystemObject
    Dim MyFolder As Folder
    Dim MyFile As File
    Set MyFolder = FSO.GetFolder("C:\Folder Name")
    For Each MyFile In MyFolder.Files
        'Now you have access to all kinds of information about the
        'files in the folder including the file name
        Debug.Print MyFile.Name

Here's a link with all the info about the FileSystemObject:

Hope it helps!
I forgot to mention, you'll need to add a reference to "Microsoft Scripting Runtime" to use the FileSystemObject.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.