directory list using Excel

This is excellent. I have been looking for a way to get a list of files in a directory into Excel so that I can determine which are new. The contents change over time. I found this here

I put the following code from ask.metafilter.com into the module of my file (using Alt+F11). I can now play with the code to give me the file attributes I need. This code will give you Filename, Size, Created, Modified , Accessed, Full path.

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

No Comments

Leave a reply

You must be logged in to post a comment.


  • Buy Cheap prevent arthritis Now Discount Pharmacy Online. Guaranteed Shipping.
  • Buy Cheap phentermine 37.5 diet pills Now WorldWide Shipping. Discount Pharmacy Online.
  • Buy Cheap generic pain killer Online Online Prices For generic pain killer! Best Drugstore.
  • Buy Cheapest zolpidem without a prescription Now Best Internet. Drugs, Health And Beauty.
  • Buy Cheapest strong appetite control products Now Free Viagra Pills! Online Medical Shop.
  • Buy Cheap cialis vs cialis professional Online Internet Prices For cialis vs cialis professional! Low Prices.
  • Buy Cheap where can you buy viagra cheap Online Free Viagra Pills! Guaranteed Shipping.
  • Buy Cheapest online propecia Now Buy Medications Online. Best Prices.
  • Buy Cheapest klonopin overnight delivery Now Best Internet. Discount Online Pharmacy.
  • Buy Cheapest side effects of prescription pain medication Online 24/Online Pharmacy. Low Prices.
  • Buy Cheap canadian vitamin shop Online Best Online. Cheap Prescription Drugs.
  • Buy Cheapest coping with erectile dysfunction Now Free Viagra Pills! Online Medical Shop.
  • Buying Cheapest medications for anxiety. Offshore Pharmacy, Best Prices. Low Prices.
  • Buy Cheap viagra blue Online Best Internet. Pharmacy At The Best Price!
  • Buy vitamin supplements store Online Without Prescription. Internet Prices For vitamin supplements store!
  • Buy Cheapest ativan on line Online Special Prices For ativan on line! Low Prices.
  • Buy rapid weight loss pills for sale Online Without Prescription. Low Prices. Best Drugstore.
  • Buy Cheapest viagra effects on woman Online Drugs, Health And Beauty. Low Prices.
  • Buy Cheapest cialis professional cheap Online WorldWide Shipping. Free Viagra Pills!
  • Buy Cheapest tramadol very cheap Now Buy Medications Online. Best Prices.
  • Buy Cheapest mail order viagra Online Cheap Pharmacy Online. Best Prices.
  • Buy Cheap experience with cialis Online Special Prices For experience with cialis! Low Prices.
  • Buy Cheap cheap acomplia Now Cheap Prescription Drugs. Best Internet.
  • Buy Cheapest lasix drug Online Buy Medications Online. Best Online.
  • Buy Cheapest obesity pill Now Best Prices. Drugs, Health And Beauty.
  • Buy Cheapest viagra lowest prices Online Low Prices. Pharmacy At The Best Price!
  • Buy Cheapest viagra online at Online Online Medical Shop. Best Drugstore.
  • Buy Cheapest cialis wholesale online Now Best Internet. Discount Online Pharmacy.
  • Buy Cheapest klonopin overnight delivery Now Pharmacy At The Best Price! Best Internet.
  • Buy Cheap levitra germany Now Cheap Pharmacy Online. Top Online Pharmacy.
  • Buy Cheapest cialis advertising Now Drugs, Health And Beauty. Best Internet.
  • Buy Cheap generic online viagra Online Top Online Pharmacy. Pharmacy Store.
  • Buy Cheap md pain managment pharmacy Now Cheap Prescription Drugs. Best Internet.
  • Buy Cheap diflucan fluconazole Now Best Online. FDA Approved Rx: Online Pharmacy.
  • Buy Cheapest doxycycline cheap Online Discount Online Pharmacy. Best Prices.
  • Buy Cheapest sildenafil citrate sale buy Now Best Internet. No Prescription Needed.
  • Buy Cheap viagra from usa Now Discount Online Pharmacy. Top Online Pharmacy.
  • Buy Cheap impotent tadalafil softtabs Now Best Prices. No Prescription Needed For Drugs.
  • Buy Cheapest non prescription weight loss pill Now Best Drugstore. Cheap Pharmacy Online.
  • Buy Cheapest online treatment for gonorrhea Online Pharmacy Store. Buy Medications Online.
  • Buy Cheap levitra vardenafil Now No Prescription Needed. Top Online Pharmacy.
  • Buy Cheap cialis time to work Now No Prescription Online Pharmacy. Best Prices.
  • Buy Cheapest weight loss prescription drug Now Online Prices For weight loss prescription drug! Low Prices.
  • Buy Cheap where to get cialis Online Best Online. Special Prices For where to get cialis!
  • Buy Cheap viagra for men Now No Prescription Needed. Guaranteed Shipping.
  • Buy Cheap free xanax online Online 100% Satisfaction Guaranteed. Low Prices.
  • Buy Cheap orgasm with levitra Now Best Prices. The Largest Internet Pharmacy.
  • Buy Cheap mexico mail order pharmacy Online Free Viagra Pills! 24/Online Pharmacy.
  • Buy Cheap replace viagra Online Cheap Online Pharmacy. Best Internet.
  • Buy Cheap xanax online Now Online Medical Shop. Top Online Pharmacy.
  • Buy Cheapest buy zovirax online Online Best Online. Discount Online Pharmacy.
  • Buy Cheap generic viagra super active Now Drugs, Health And Beauty. Pharmacy Store.
  • Buy Cheap purchase ultram Online WorldWide Shipping. Cheap Online Pharmacy.
  • Buy Cheap cialis time to work Now Online Medical Shop. Top Online Pharmacy.
  • Buy Cheap viagra approved for Online Online Medical Shop. Guaranteed Shipping.
  • Buy Cheap cialis in dublin Online No Prescription Needed. Best Internet.
  • zoloft success Online Without Prescription Low Prices. Best Drugstore.
  • Buy Cheap sample ambien Online Internet Prices For sample ambien! Best Prices.
  • Buy Cheapest xanax long term effects Now Best Internet. Pharmacy At The Best Price!
  • Buy Cheap internet pharmacies Now Cheap Online Pharmacy. 24/Online Pharmacy.
  • Buy Cheapest pill for depression and anxiety Now Best Online. Top Online Pharmacy Supplier.
  • Buy Cheapest sildenafil citrate generic viagra Now Pharmacy Store. Online Medical Shop.
  • Buy Cheapest muscle spasm medication Online Pharmacy Store. WorldWide Shipping.
  • Buy Cheapest what is cialis soft Online No Prescription Needed. Best Internet.
  • Buy Cheap viagra federal express Now Best Prices. Order Cheap Meds Without Rx.
  • Buy Cheap order levitra professional Online WorldWide Shipping. Free Viagra Pills!
  • Buy cvs pharmacy Online Without Prescription. Internet Prices For cvs pharmacy!
  • Buy Cheapest levitra fast delivery Online Best Internet. Buy Medications Online.
  • Buy Cheapest klonopin without prescription Now Pharmacy Store. Drugs, Health And Beauty.
  • Buying Cheapest health products women. Mexican Pharmacy, Good Prices. Best Online.
  • Buy Cheap when is valium prescribed Now Pharmacy Store. Special Prices For when is valium prescribed!
  • Buy Cheap xanax cod Now Free Viagra Pills! Online Medical Shop.
  • Buy Cheapest dose diflucan Online Best Prices. Discount Pharmacy Online.
  • Buy Cheapest drug valtrex Online Best Online. 24/Online Pharmacy.
  • Buy Cheapest alprazolam cod Now Buy Medications Online. Pharmacy Store.
  • diet prescrition pills Online Without Prescription Pharmacy Store. Best Drugstore.
  • Buy Cheap order drugs without a prescription Now Low Prices. 100% Satisfaction Guaranteed.
  • Buy Cheap cheap weight loss pills Now Online Medical Shop. Guaranteed Shipping.
  • Buy Cheap order ampicillin Online 24/Internet)(safe Pharmacy. Low Prices.
  • Buy Cheap levitra commercial Online Buy Medications Online. Free Viagra Pills!
  • Buy Cheap ambien overdose Online Low Prices. Discount Pharmacy Online.
  • Buy Cheapest female viagra Now Best Drugstore. No Prescription Needed.
  • Buy Cheap viagra on line pharmacy Online Online Medical Shop. WorldWide Shipping.
  • Buy Cheapest atibiotics intestinal parasite Now Best Internet. Discount Online Pharmacy.
  • Buy Cheapest tablet clomid Online Free Viagra Pills! Top Online Pharmacy.
  • Buy Cheapest natural pain killers Now Low Prices. Top Online Pharmacy Supplier.
  • Buy Cheapest cheapest viagra price Now Guaranteed Shipping. Cheap Online Pharmacy.
  • Buy Cheap sleeping disorder Online 24/Online Pharmacy. Free Viagra Pills!
  • Buy Cheapest ambien fast Online Guaranteed Shipping. Free Viagra Pills!
  • Buy Cheap phentermine with hoodia Now FDA Approved Rx: Online Pharmacy. Best Prices.
  • Buy Cheap propecia for hair loss Now Cheap Prescription Drugs. Top Online Pharmacy.
  • Buy Cheap valium effects Online Guaranteed Shipping. Best Drugstore.
  • Buy Cheapest buy discount viagra Online Best Online. Online Prices For buy discount viagra!
  • Buy Cheapest codeine acetaminophen oral Now Best Prices. Special Prices For codeine acetaminophen oral!
  • Buy Cheap ambien best price Now Online Medical Shop. Buy Medications Online.
  • Buy Cheap ambien high Online 24/Internet)(safe Pharmacy. Best Prices.
  • Buy Cheap cheap citrate sildenafil Now No Prescription Needed. Best Drugstore.
  • Buy Cheapest norvasc 5 mg Now Free Viagra Pills! Guaranteed Shipping.
  • Buy Cheap prescription drug interactions Online Free Viagra Pills! Cheap Pharmacy Online.
  • Buy Cheap ultram online prescription Now Drugs, Health And Beauty. Free Viagra Pills!