+ Reply to Thread
Results 1 to 6 of 6

Want to Pull File Names From Folder into Excel

Hybrid View

  1. #1
    Registered User
    Join Date
    09-05-2012
    Location
    Vancouver
    MS-Off Ver
    Excel 2010
    Posts
    2

    Want to Pull File Names From Folder into Excel

    Hi,

    I'm looking for a way to import the names of all the files in a particular folder into excel.

    The directory will always be the same so I want it re-scanned automatically every time I open the spreadsheet and then for the names of the files to be imported. Is there an easy way to do this? I've tried grabbing snippets of vba code and fiddling, but nothing is working quite right.

    In addition to that, once all of the file names are imported I am wondering if it is possible to delete entire rows based on the extensions listed (ex. ".xml")? Many files are listed two or three times in the directory and I only need ones with certain extensions.

    Thanks in advance for any help. I greatly appreciate it!
    Last edited by lytsty; 09-07-2012 at 12:58 AM.

  2. #2
    Registered User
    Join Date
    08-31-2012
    Location
    Andromeda, Hawaii
    MS-Off Ver
    Excel 97/2003 and 5.0/97
    Posts
    49

    Re: Want to Pull File Names From Folder into Excel

    hope this help.

    Sub ListAllFiles()
    
    'Set a reference to 'Microsoft Scripting Runtime' under menu option:
    ' Tools > References > find and tick 'Microsoft Scripting Runtime'
    
    Dim fso As New FileSystemObject
    Dim fso_Folder As Folder
    Dim fso_File As File
    Dim file_count As Long
    
    'Change the directory below to the one you want to search
    Set fso_Folder = fso.GetFolder("C:\")
    file_count = 0
    
    For Each fso_File In fso_Folder.Files
    file_count = file_count + 1
    Cells(file_count, 1).Value = fso_File.Name
    Next fso_File
    
    Set fso = Nothing
    
    End Sub
    this module is to list all the files in the specified directory.
    Last edited by jdlc; 09-05-2012 at 09:49 PM.

  3. #3
    Registered User
    Join Date
    08-31-2012
    Location
    Andromeda, Hawaii
    MS-Off Ver
    Excel 97/2003 and 5.0/97
    Posts
    49

    Re: Want to Pull File Names From Folder into Excel

    here you go boss.


    Sub ListAllFiles()
    
    'Set a reference to 'Microsoft Scripting Runtime' under menu option:
    ' Tools > References > find and tick 'Microsoft Scripting Runtime'
    
    Dim fso As New FileSystemObject
    Dim fso_Folder As Folder
    Dim fso_File As File
    Dim file_count As Long
    Dim strExt As String
    
    'Change the directory below to the one you want to search
    Set fso_Folder = fso.GetFolder("C:\")
    file_count = 0
    
    'Here's your file extension
    strExt = "xls"
    
    For Each fso_File In fso_Folder.Files
    
    If UCase(Right(fso_File.Name, (Len(fso_File.Name) - InStrRev(fso_File.Name, ".")))) = UCase(strExt) Then
        file_count = file_count + 1
        Cells(file_count, 1).Value = fso_File.Name
    End If
    
    Next fso_File
    
    Set fso = Nothing
    
    End Sub
    Last edited by jdlc; 09-05-2012 at 10:22 PM.

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Want to Pull File Names From Folder into Excel

    Hi lytsty,

    Welcome to the forum!!

    This should do the job:

    Option Explicit
    Sub Macro1()
        
        'http://www.excelforum.com/excel-programming-vba-macros/858256-want-to-pull-file-names-from-folder-into-excel.html
        
        'List all file names with extensions not found in 'vayMyArray' residing in the 'strMyDir' directory.
        
        Dim strMyDir As String
        Dim varMyArray As Variant
        Dim objFSO As Object, _
            objFile As Object
        Dim lngMyRow As Long
        
        strMyDir = "G:\" 'Directory to look in. Don't forget the trailing backslash.
        varMyArray = Array("xml", "txt") 'Files with these extensions will be ignorged (i.e. not imported).
        
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        
        'Clear any existing imported file names from Col. A (starting at Row 2) from Sheet1. Change to suit.
        lngMyRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
        If lngMyRow >= 2 Then
            Sheets("Sheet1").Range("A2:A" & lngMyRow).ClearContents
        End If
        
        lngMyRow = 0 'Initialise variable.
            
        For Each objFile In objFSO.GetFolder(strMyDir).Files
            If IsNumeric(Application.Match(objFSO.GetExtensionName(strMyDir & objFile.Name), varMyArray, 0)) = False Then
                If lngMyRow = 0 Then 'Initial row number output. Change to suit.
                    lngMyRow = 2
                Else
                    lngMyRow = lngMyRow + 1
                End If
                Sheets("Sheet1").Range("A" & lngMyRow).Value = objFile.Name
            End If
        Next objFile
        
        Set objFSO = Nothing
        Set objFile = Nothing
            
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  5. #5
    Registered User
    Join Date
    09-05-2012
    Location
    Vancouver
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Want to Pull File Names From Folder into Excel

    Worked like a charm.

    Thanks Everyone!!

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Want to Pull File Names From Folder into Excel

    Thanks for letting is know and you're welcome. If you could mark the thread as solved it would be appreciated.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1