Solved

Get file name

Posted on 2003-10-23
6
161 Views
Last Modified: 2011-04-14
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
Comment
Question by:bsamaan
  • 4
  • 2
6 Comments
 
LVL 9

Expert Comment

by:Dang123
ID: 9608166
Look at the Dir Function in VB help
0
 
LVL 9

Expert Comment

by:Dang123
ID: 9608275
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
 
LVL 9

Expert Comment

by:Dang123
ID: 9608283
You could save the returned string to a file or work with it as you need.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Accepted Solution

by:
Dang123 earned 250 total points
ID: 9608310
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
 
LVL 5

Expert Comment

by:JCinDE
ID: 9608327
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
 
LVL 5

Expert Comment

by:JCinDE
ID: 9608341
I forgot to mention, you'll need to add a reference to "Microsoft Scripting Runtime" to use the FileSystemObject.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Determine Range to Select 5 42
VBA filters 2 50
Copy a row 12 57
Problem to With line 4 37
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now