+ Reply to Thread
Results 1 to 6 of 6

Printing File Contents to an Excel Spreadsheet

Hybrid View

  1. #1
    Registered User
    Join Date
    07-02-2010
    Location
    Mississauga, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    8

    Printing File Contents to an Excel Spreadsheet

    I did a small search here and on the internet thats yielded a couple results.

    I was just wondering if anyone knows a sure fire way that I can list the contents of my music folder (K:\Music) on an excel spreadsheet?

    Please and thanks

  2. #2
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Printing File Contents to an Excel Spreadsheet

    In the command window (DOS), you can do a

    dir > mymusic.txt

    then open the mymusic.txt with Excel

  3. #3
    Registered User
    Join Date
    07-02-2010
    Location
    Mississauga, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Printing File Contents to an Excel Spreadsheet

    I'm running windows 7, whenever I do the dir > mymusic.txt I get access denied.

    in the cmd prompt, I can get it to go to C, but it won't let me ** K:\Music to my music directory

  4. #4
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Printing File Contents to an Excel Spreadsheet

    Have you tried a search? There are lots of hits for a google

    excel macro to list files in a folder

  5. #5
    Registered User
    Join Date
    07-02-2010
    Location
    Mississauga, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Printing File Contents to an Excel Spreadsheet

    I tried numerous google search results.

    This seems to be the closest I got only because it actually listed the headings, but no files before I get an error

    Sub TestListFilesInFolder()
        Workbooks.Add ' create a new workbook for the file list
        ' add headers
        With Range("A1")
            .Formula = "Folder contents:"
            .Font.Bold = True
            .Font.Size = 12
        End With
        Range("A3").Formula = "File Name:"
        Range("B3").Formula = "File Size:"
        Range("C3").Formula = "File Type:"
        Range("D3").Formula = "Date Created:"
        Range("E3").Formula = "Date Last Accessed:"
        Range("F3").Formula = "Date Last Modified:"
        Range("G3").Formula = "Attributes:"
        Range("H3").Formula = "Short File Name:"
        Range("A3:H3").Font.Bold = True
        ListFilesInFolder "C:\FolderName\", True 
        ' list all files included subfolders
    End Sub
    
    Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
    ' lists information about the files in SourceFolder
    ' example: ListFilesInFolder "C:\FolderName\", True
    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 properties
            Cells(r, 1).Formula = FileItem.Path & FileItem.Name
            Cells(r, 2).Formula = FileItem.Size
            Cells(r, 3).Formula = FileItem.Type
            Cells(r, 4).Formula = FileItem.DateCreated
            Cells(r, 5).Formula = FileItem.DateLastAccessed
            Cells(r, 6).Formula = FileItem.DateLastModified
            Cells(r, 7).Formula = FileItem.Attributes
            Cells(r, 8).Formula = FileItem.ShortPath & FileItem.ShortName
            ' use file methods (not proper in this example)
    '        FileItem.Copy "C:\FolderName\Filename.txt", True
    '        FileItem.Move "C:\FolderName\Filename.txt"
    '        FileItem.Delete True
            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:H").AutoFit
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
        ActiveWorkbook.Saved = True
    End Sub
    Do I change every C:\ to K:\Music?

  6. #6
    Registered User
    Join Date
    07-02-2010
    Location
    Mississauga, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Printing File Contents to an Excel Spreadsheet

    I tried numerous google search results.

    This seems to be the closest I got only because it actually listed the headings, but no files before I get an error

    Sub TestListFilesInFolder()
        Workbooks.Add ' create a new workbook for the file list
        ' add headers
        With Range("A1")
            .Formula = "Folder contents:"
            .Font.Bold = True
            .Font.Size = 12
        End With
        Range("A3").Formula = "File Name:"
        Range("B3").Formula = "File Size:"
        Range("C3").Formula = "File Type:"
        Range("D3").Formula = "Date Created:"
        Range("E3").Formula = "Date Last Accessed:"
        Range("F3").Formula = "Date Last Modified:"
        Range("G3").Formula = "Attributes:"
        Range("H3").Formula = "Short File Name:"
        Range("A3:H3").Font.Bold = True
        ListFilesInFolder "C:\FolderName\", True 
        ' list all files included subfolders
    End Sub
    
    Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
    ' lists information about the files in SourceFolder
    ' example: ListFilesInFolder "C:\FolderName\", True
    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 properties
            Cells(r, 1).Formula = FileItem.Path & FileItem.Name
            Cells(r, 2).Formula = FileItem.Size
            Cells(r, 3).Formula = FileItem.Type
            Cells(r, 4).Formula = FileItem.DateCreated
            Cells(r, 5).Formula = FileItem.DateLastAccessed
            Cells(r, 6).Formula = FileItem.DateLastModified
            Cells(r, 7).Formula = FileItem.Attributes
            Cells(r, 8).Formula = FileItem.ShortPath & FileItem.ShortName
            ' use file methods (not proper in this example)
    '        FileItem.Copy "C:\FolderName\Filename.txt", True
    '        FileItem.Move "C:\FolderName\Filename.txt"
    '        FileItem.Delete True
            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:H").AutoFit
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
        ActiveWorkbook.Saved = True
    End Sub
    Do I change every C:\ to K:\Music?

+ 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