Help me output a list of files in a directory to Excel or HTML...
January 13, 2007 8:17 AM   Subscribe

How can I output a list of files in a specified folder, including certain file attributes (i.e. date created, date modified, etc), to a HTML or spreadsheet file? I'm using Windows XP.

I know it's a simple question, but all of the search terms I've used are too common to yield good results, and some of the software I've found appears to be somewhat "sketchy".

I need a piece of software (or some kind of extension to Windows Explorer) that will allow me to output a list of files in a given directory, including the file name, date created, file comments, and any other attributes tied to the file. Ideally the output would be in a spreadsheet or HTML format.

Is there a (preferably free) piece of software out there that accomplishes this task?
posted by gwenzel to Computers & Internet (11 answers total)
 
If you drop to a command line and run the following:

dir C:\mydirectory\*.* >>foo.txt

... it will output the directory listing to a fixed-width text file, which you can then bring into Excel and manipualte to your heart's content. If you want an automated solution to do this on a regular basis, it would be a five-minute job to write either a piece of VBA (for Excel format) or Perl (for HTML) that can output it directly in the format you want.
posted by Doofus Magoo at 8:47 AM on January 13, 2007


Search for freeware called FolderPrint. Last I saw, it wasn't especially sophisticated or complex. I don't recall what file attributes it listed.
posted by Dave 9 at 10:54 AM on January 13, 2007


Response by poster: Doofus Magoo: Something like dir in a batch file was what I was thinking of, but I can't figure out how to get dir to output file comments. I know nothing of VBA or Perl, unfortunately.

Dave 9: I tried Folderprint, but for some reason it won't extract file comments properly, and it costs $22, which seems high to me for what I'm looking for.

Any other ideas? Any VBA or Perl experts out there that want to help out a fellow mefite?
posted by gwenzel at 11:39 AM on January 13, 2007


Missed that little chunk of your original request. What do you mean by "file comments?"
posted by Doofus Magoo at 12:54 PM on January 13, 2007


Response by poster: If you right-click on a file in Windows, and select "properties", and click on the "summary" tab, a number of options are available.

I'm planning on using the "comments" (or perhaps the "title") field to enter a description for each document (a PDF file). By the time I'm done, I expect to have at least a few thousand PDF files, and using one of the file attribute fields allows me to keep the files indexed without maintaining a separate list of documents.

I just need a way to extract that data, along with the file name and "file created" dates, and export it to a format that can be easily manipulated, printed, or whatnot.
posted by gwenzel at 1:07 PM on January 13, 2007


Best answer: If you paste the below code into an Excel VBA module (ALT F11, right click and insert new module and paste this code there). Then go back to your main Excel window and hit ALT F8. This should let you run the macro GetFileList.

What this doesn't give you is the file comments. THis can be done but requires dsofile.dll to be installed on your PC.

Post back if you need that too.....


Option Explicit
Sub GetFileList()

Dim strFolder As String
Dim varFileList As Variant
Dim FSO As Object, myFile As Object
Dim myResults As Variant
Dim l As Long

' Get the directory from the user
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
If .SelectedItems.Count = 0 Then Exit Sub 'user cancelled
strFolder = .SelectedItems(1)
End With

' Get a list of all the files in this directory.
' Note that this isn't recursive... although it could be...
varFileList = fcnGetFileList(strFolder)

If Not IsArray(varFileList) Then
MsgBox "No files found.", vbInformation
Exit Sub
End If

' Now let's get all the details for these files
' and place them into an array so it's quick to dump to XL.
ReDim myResults(0 To UBound(varFileList) + 1, 0 To 5)

' place make some headers in the array
myResults(0, 0) = "Filename"
myResults(0, 1) = "Size"
myResults(0, 2) = "Created"
myResults(0, 3) = "Modified"
myResults(0, 4) = "Accessed"
myResults(0, 5) = "Full path"

Set FSO = CreateObject("Scripting.FileSystemObject")

' Loop through our files
For l = 0 To UBound(varFileList)
Set myFile = FSO.GetFile(CStr(varFileList(l)))
myResults(l + 1, 0) = CStr(varFileList(l))
myResults(l + 1, 1) = myFile.Size
myResults(l + 1, 2) = myFile.DateCreated
myResults(l + 1, 3) = myFile.DateLastModified
myResults(l + 1, 4) = myFile.DateLastAccessed
myResults(l + 1, 5) = myFile.path
Next l

' Dump these to a worksheet
fcnDumpToWorksheet myResults

'tidy up
Set myFile = Nothing
Set FSO = Nothing


End Sub

Private Function fcnGetFileList(ByVal strPath As String, Optional strFilter As String) As Variant
' Returns a one dimensional array with filenames
' Otherwise returns False

Dim f As String
Dim i As Integer
Dim FileList() As String

If strFilter = "" Then strFilter = "*.*"

Select Case Right$(strPath, 1)
Case "\", "/"
strPath = Left$(strPath, Len(strPath) - 1)
End Select

ReDim Preserve FileList(0)

f = Dir$(strPath & "\" & strFilter)
Do While Len(f) > 0
ReDim Preserve FileList(i) As String
FileList(i) = f
i = i + 1
f = Dir$()
Loop

If FileList(0) <> Empty Then
fcnGetFileList = FileList
Else
fcnGetFileList = False
End If
End Function
Private Sub fcnDumpToWorksheet(varData As Variant, Optional mySh As Worksheet)

Dim iSheetsInNew As Integer
Dim sh As Worksheet, wb As Workbook
Dim myColumnHeaders() As String
Dim l As Long, NoOfRows As Long

If mySh Is Nothing Then

'make a workbook if we didn't get a worksheet
iSheetsInNew = Application.SheetsInNewWorkbook
Application.SheetsInNewWorkbook = 1
Set wb = Application.Workbooks.Add
Application.SheetsInNewWorkbook = iSheetsInNew
Set sh = wb.Sheets(1)

Else

Set mySh = sh

End If

With sh

Range(.Cells(1, 1), .Cells(UBound(varData, 1) + 1, UBound(varData, 2) + 1)) = varData
.UsedRange.Columns.AutoFit

End With

Set sh = Nothing
Set wb = Nothing

End Sub
posted by NailsTheCat at 2:05 PM on January 13, 2007 [1 favorite]


Response by poster: NailsTheCat: Thanks very much - that looks like what I need. I don't have the dsofile.dll though. I've done alt-F11 in Excel, but it doesn't seem to do anything. I'm fairly computer literate, but scripting isn't something that I have any experience with... can you break down the instructions a little more? Thanks again!
posted by gwenzel at 5:37 PM on January 13, 2007


The above code just gave you the basic file info. It didn't give you the Title etc. I've added the code to do that. You can download an Excel workbook from here to see it in action. Just click the big grey button.

It will work without dsofile.dll but it won't give you the extra fields you want - no way around that as far as I can tell. So you can just d/l that too (from MS). (Just doubleclick to install. You can leave as is in its default directory or move the dsofile.dll to c:\windows\system32\ and delete the other example files it installs.)

Once you have dsofile.dll on your 'puter you should get a list of all your files in the selected directory plus those Title, Comments, Subject fields too.

I don't know why ALT+F11 doesn't work for you :( but you can also access the VBE from Tools \ Macros \ Visual Basic Editor. That's where you would have inserted the above code. (Not that you need to now you can d/l the workbook.)

Note, the workbook (obviously) contains macros. So you may need to change your security options (Tools \ Macros \ Security) to Medium and then open the file, clicking on enable if you get the security prompt. (You can always open disabled first, open the VBE and examine the code to check there's nothing nefarious in there - can't be too careful!)

HTH - post back if it doesn't (or email in profile). It's actually quite neat - I may use this myself!!
posted by NailsTheCat at 11:15 PM on January 13, 2007


OK - and of course you can also use DSO to write back to the file too. So you can update your Title fields etc. in Excel and then save that back to the file. Just tested it (code is in the file). I'd have to make it a little more user friendly for ya.

If you want to test that for now, just select a row (or cell) on the spreadsheet row of the file you want to update, press ALT+F8 and select 'UpdateFileInfo' and click Run. I'm definitely going to use this now!!
posted by NailsTheCat at 11:37 PM on January 13, 2007


Response by poster: NailsTheCat: That looks awesome, but I'm getting a glitch - I've figured out the alt-F11 problem (just a keyboard setting that had F-keys disabled) and entered the code into Excel. I've downloaded your sample worksheet, and opened it in Excel; problem is, the grey button gives an error - "Compile error: variable not defined". Any ideas?

I'm using Excel 2000, if that makes a difference.

I haven't got to the point of having this work yet, but it sounds exactly like what I'm looking for. Thanks so much for your efforts thus far!

If you want to move this to e-mail, my address is also in my profile.
posted by gwenzel at 3:40 AM on January 14, 2007


It turns out XL2000 does make a difference - it doesn't support the 'browse for folder' dialogue I had used. I've modified it to use a generic windows browse now.

I'll send via email.
posted by NailsTheCat at 9:07 AM on January 14, 2007


« Older Down payment blues   |   Right click, save link target as? Newer »
This thread is closed to new comments.