Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Generate a directory/folder listing

1 view
Skip to first unread message

Albert D. Kallal

unread,
May 31, 2004, 5:28:08 PM5/31/04
to
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 D. Kallal (Access MVP)
Edmonton, Alberta Canada
pleaseNOO...@msn.com
http://www.attcanada.net/~kallal.msn

LenP (again)

unread,
Jun 1, 2004, 8:41:03 AM6/1/04
to
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

LenP (ignore the first reply!)

unread,
Jun 1, 2004, 1:26:03 PM6/1/04
to
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 D. Kallal

unread,
Jun 1, 2004, 3:24:13 PM6/1/04
to
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..

John Vinson

unread,
Jun 1, 2004, 4:23:19 PM6/1/04
to
On Tue, 1 Jun 2004 13:24:13 -0600, "Albert D. Kallal"
<PleaseNOOO...@msn.com> wrote:

>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.

John W. Vinson[MVP]
Come for live chats every Tuesday and Thursday
http://go.compuserve.com/msdevapps?loc=us&access=public

LenP

unread,
Jun 3, 2004, 10:36:03 AM6/3/04
to
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 colFolders
If (GetAttr(strStartDir + varFolderName) And vbDirectory) = vbDirectory Then
Call FillDir(strStartDir & varFolderName & "\", colDirList)
End If
Next varFolderName

Sergej Berner

unread,
Jun 7, 2004, 10:43:03 AM6/7/04
to

"LenP" <anon...@discussions.microsoft.com> schrieb im Newsbeitrag
news:81440426-D8B6-440D...@microsoft.com...
> Brilliant!!!
>


Bud T

unread,
Jul 15, 2004, 8:46:03 PM7/15/04
to
Hi Albert, I just ran across this post and found it very useful. Thanks. I have been trying to get it to just get certain file extensions (i.e. DLL) . I would also like to be able to pass the directory name from a form to the code or better yet have a windows style browse on a form. Do you have any ideas?

Thanks,
Bud

Douglas J. Steele

unread,
Jul 16, 2004, 12:58:24 PM7/16/04
to
To pass a directory name, change the sub declaration in Albert's code from

Sub dirTest()

to

Sub dirTest(startDir As String)

and remove the line

startDir = "C:\access\"

from the body of the code.

To invoke a browser to retrieve the folder name, take a look at
http://www.mvps.org/access/api/api0002.htm at "The Access Web"

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(No private e-mails, please)


"Bud T" <Bu...@discussions.microsoft.com> wrote in message
news:E1467837-EB09-46B8...@microsoft.com...

0 new messages