• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

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,
0
bsamaan
Asked:
bsamaan
  • 4
  • 2
1 Solution
 
Dang123Commented:
Look at the Dir Function in VB help
0
 
Dang123Commented:
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
            Else
                strReturn = strReturn & strWork & vbCrLf
            End If
        End If
        strWork = Dir   ' Get next entry.
    Loop
    ListFiles = strReturn
End Function
0
 
Dang123Commented:
You could save the returned string to a file or work with it as you need.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Dang123Commented:
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.)
0
 
JCinDECommented:
If you just need a text file, you can use this command at a DOS prompt:

  DIR /B > DIRCONTENTS.TXT

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
    Next

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

    http://msdn.microsoft.com/library/en-us/script56/html/FSOoriFileSystemObject.asp

Hope it helps!
-JC
0
 
JCinDECommented:
I forgot to mention, you'll need to add a reference to "Microsoft Scripting Runtime" to use the FileSystemObject.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now