Results 1 to 16 of 16

Pull File Names into Excel and replace dir

Threaded View

  1. #1
    Registered User
    Join Date
    07-05-2011
    Location
    Toledo, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    10

    Pull File Names into Excel and replace dir

    I am still a beginner with VB. With that stated, I am creating a vb script to help make part of my job a little easier but am havign a slight issue. I need pull all the file names from a directory (including subfolders) have them output in a column in Excel 2007. I get the first part to work GREAT, BUT my main issue is that when it outputs the list I need to replace "C:\" with "\\abcdefg123\" . I need just that part replaced with the rest of the file path following.
    Sub FileNames()
        
        ListFilesInFolder "C:\<parent folder>\<subfolder>\<file>", True  
        
    End Sub
    
    Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
    
    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
    Dim FileItem As Scripting.File
    Dim r As Long
    
        Set FSO = New Scripting.FileSystemObject
        Set SourceFolder = FSO.GetFolder(SourceFolderName)
        r = Range("A65536").End(xlUp).Row + 1
        
        For Each FileItem In SourceFolder.Files
            ' display file path
            Cells(r, 1).Formula = FileItem.Path
            r = r + 1 ' next row number
        Next FileItem
        If IncludeSubfolders Then
            For Each SubFolder In SourceFolder.SubFolders
                ListFilesInFolder SubFolder.Path, True
            Next SubFolder
            
        End If
        Columns("A:Z").AutoFit
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
        ActiveWorkbook.Saved = True
                
    End Sub
    Last edited by Leith Ross; 07-05-2011 at 11:47 PM. Reason: Added Code Tags

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