Generate a directory/folder listing

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to generate a table containing all files (path included) older than a specific date. I am able to get some folder/file names, however the code I've come up with only allows me to get the info from the default folder that Ive specified; it doesn't drill down into the subfolders

Anyone have any suggestions

TIA, Le
 
You could use the following code:

Sub dirTest()

Dim dlist As New Collection
Dim startDir As String
Dim i As Integer

startDir = "C:\access\"
Call FillDir(startDir, dlist)

MsgBox "there are " & dlist.Count & " in the dir"

' lets printout the stuff into debug window for a test

For i = 1 To dlist.Count
Debug.Print dlist(i)
Next i

' in place of the above..you could use the follwing to add the data to a
table:

' lets add the results to a table
dim rstRecs as dao.recordset
set rstRecs = currentdb.OpenRecordSet("tblDirs")

For i = 1 To dlist.Count
rstRecs.AddNew
rstRecs!FileName = dlist(i)
rstRecs!????Date = filedateTime(dlist(i))
rstRecs.Update
next i
rstRecs.Close
set rstRecs = nothing
Next i
End Sub


Sub FillDir(startDir As String, dlist As Collection)

' build up a list of files, and then
' add add to this list, any additinal
' folders

Dim strTemp As String
Dim colFolders As New Collection
Dim vFolderName As Variant

strTemp = Dir(startDir)

Do While strTemp <> ""
dlist.Add startDir & strTemp
strTemp = Dir
Loop

' now build a list of additional folders
strTemp = Dir(startDir & "*.", vbDirectory)

Do While strTemp <> ""
If (strTemp <> ".") And (strTemp <> "..") Then
colFolders.Add strTemp
End If
strTemp = Dir
Loop

' now process each folder (recursion)
For Each vFolderName In colFolders
Call FillDir(startDir & vFolderName & "\", dlist)
Next vFolderName

End Sub
 
Albert

You're a genius, that looks great!!

I did come across a few gliches, i.e. "User-defined type not defined" error on the 'Dim rstRecs As DAO.Recordset' line
Is there some compatibility issue in Access 2000 for this statement? Bt changing it to adoDB.recordset I get rid of the error but get a "type mismatch" error on 'Set rstRecs = CurrentDb.OpenRecordSet("tblFileList")'

Any thoughts

Thanks
Le
 
DOH
Sorry to be a pest, there is one other thing that seems to cause a hiccup... it chokes on files that have no exension. You get the following: "Bad file name or number"

I know I shouldn't expect you to write this code for me; if I wasn't such a newbie at this, I wouldn't be on this Newsgroup

Gotta start somewhere <gr>

Thanks
Len
 
ALbert

I managed to get around the DB issues I was having, just took a little looking into. I do however have an interesting thing happening..
after insertng six records into the recordset, the code blows up with "The field is to small to accept the amount of data you attempted to add. Try inserting or pasting less data." The data is fairly consistent in size. Any thoughts
Thanks, Len
 
Albert

I've got this thing nailed down to one issue and that is that when it comes across a file that has no extension, it treats it as a folder and chokes on "RunTime error '52': Bad file name or number"

Any thoughts

Cheers
Len
 
Try putting a on error resume next *right* before the first dir command....


on error resume next <----------

strTemp = Dir(startDir)

Do While strTemp <> ""
dlist.Add startDir & strTemp
strTemp = Dir
Loop

That is cheap solution...but it should work!

Also, you should note that this program technique of a program calling its
self over and over is that we call recursion. Recursion is rather very cool
code solution to many types of computer problems. Recursion (having code
call and re-use its self) can save a LOT of code, and can solve some really
cool things.

As for your other questions...I assume you figured out to add the dao 3.6
reference to my example for the reocrdset code?

While in code..use tools->references-> add the Microsoft 3.6 dao library
ref.
The field is to small to accept the amount of data you attempted to add.
Try inserting or pasting less data." The data is fairly consistent in size.

The default field size in a table is only 50 characters. You go quite far
into the directory tree..that result will be longer then 50 chars. You will
need to increase the field size in the table design mode..
 
Also, you should note that this program technique of a program calling its
self over and over is that we call recursion.

From the Programmer's Dictionary:

Recursion, n. See: Recursion.
 
Before I got your reply about the 'on error...' I used the following to check if the item returned was a folder

' now process each folder (recursion
For Each varFolderName In colFolder
If (GetAttr(strStartDir + varFolderName) And vbDirectory) = vbDirectory The
Call FillDir(strStartDir & varFolderName & "\", colDirList
End I
Next varFolderNam
 
Back
Top