Allen Browne code to add list files to a table dons't work well on access 2013

david_3343434

New member
Local time
Yesterday, 22:35
Joined
May 5, 2013
Messages
8
i have a code (source of this code ww.allenbrowne.com/ser-59.html) that adds all files on a specified directory and on subdirectories to a table, but for some strange reason it's works only on the c: drive like this Call ListFilesToTable("c:\", , True) but if i do the same code on another drive like that: Call ListFilesToTable("d:\", , True) i get only the files on the main d: direcotry. why it that and how can i change it ?

this is the code (UG said... Pls Use Code tags)

Code:
Public Function ListFiles(strPath As String, Optional strFileSpec As String, _
    Optional bIncludeSubfolders As Boolean, Optional lst As ListBox)
On Error GoTo Err_Handler
    'Purpose:   List the files in the path.
    'Arguments: strPath = the path to search.
    '           strFileSpec = "*.*" unless you specify differently.
    '           bIncludeSubfolders: If True, returns results from subdirectories of strPath as well.
    '           lst: if you pass in a list box, items are added to it. If not, files are listed to immediate window.
    '               The list box must have its Row Source Type property set to Value List.
    'Method:    FilDir() adds items to a collection, calling itself recursively for subfolders.
    Dim colDirList As New Collection
    Dim varItem As Variant
    
    Call FillDir(colDirList, strPath, strFileSpec, bIncludeSubfolders)
    
    'Add the files to a list box if one was passed in. Otherwise list to the Immediate Window.
    If lst Is Nothing Then
        For Each varItem In colDirList
            Debug.Print varItem
        Next
    Else
        For Each varItem In colDirList
        lst.AddItem varItem
        Next
    End If

Exit_Handler:
    Exit Function

Err_Handler:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Exit_Handler
End Function

Private Function FillDir(colDirList As Collection, ByVal strFolder As String, strFileSpec As String, _
    bIncludeSubfolders As Boolean)
    'Build up a list of files, and then add add to this list, any additional folders
    Dim strTemp As String
    Dim colFolders As New Collection
    Dim vFolderName As Variant

    'Add the files to the folder.
    strFolder = TrailingSlash(strFolder)
    strTemp = Dir(strFolder & strFileSpec)
    Do While strTemp <> vbNullString
        colDirList.Add strFolder & strTemp
        strTemp = Dir
    Loop

    If bIncludeSubfolders Then
        'Build collection of additional subfolders.
        strTemp = Dir(strFolder, vbDirectory)
        Do While strTemp <> vbNullString
            If (strTemp <> ".") And (strTemp <> "..") Then
                If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0& Then
                    colFolders.Add strTemp
                End If
            End If
            strTemp = Dir
        Loop
        'Call function recursively for each subfolder.
        For Each vFolderName In colFolders
            Call FillDir(colDirList, strFolder & TrailingSlash(vFolderName), strFileSpec, True)
        Next vFolderName
    End If
End Function

Public Function TrailingSlash(varIn As Variant) As String
    If Len(varIn) > 0& Then
        If Right(varIn, 1&) = "\" Then
            TrailingSlash = varIn
        Else
            TrailingSlash = varIn & "\"
        End If
    End If
End Function
 
Last edited by a moderator:
I followed Allen Browne's instructions and it worked fine in my Access 2013...

Returned this:-
Call ListFiles("C:\ATH_PC_Drive_W8\ATH_Access\ForumAnswers\New folder")
C:\ATH_PC_Drive_W8\ATH_Access\ForumAnswers\New folder\New Microsoft Access Database.accdb
C:\ATH_PC_Drive_W8\ATH_Access\ForumAnswers\New folder\New Microsoft Access Database.laccdb
 
For D drive (A CD player) returned:-

Call ListFiles("D:\VIDEO_TS")
D:\VIDEO_TS\VIDEO_TS.BUP
D:\VIDEO_TS\VIDEO_TS.IFO
D:\VIDEO_TS\VIDEO_TS.VOB
D:\VIDEO_TS\VTS_01_0.BUP
D:\VIDEO_TS\VTS_01_0.IFO
D:\VIDEO_TS\VTS_01_0.VOB
D:\VIDEO_TS\VTS_01_1.VOB
D:\VIDEO_TS\VTS_01_2.VOB
D:\VIDEO_TS\VTS_01_3.VOB
D:\VIDEO_TS\VTS_01_4.VOB
D:\VIDEO_TS\VTS_01_5.VOB
D:\VIDEO_TS\VTS_01_6.VOB
D:\VIDEO_TS\VTS_02_0.BUP
D:\VIDEO_TS\VTS_02_0.IFO
D:\VIDEO_TS\VTS_02_0.VOB
D:\VIDEO_TS\VTS_02_1.VOB
D:\VIDEO_TS\VTS_03_0.BUP
D:\VIDEO_TS\VTS_03_0.IFO
D:\VIDEO_TS\VTS_03_0.VOB
D:\VIDEO_TS\VTS_03_1.VOB
D:\VIDEO_TS\VTS_04_0.BUP
D:\VIDEO_TS\VTS_04_0.IFO
D:\VIDEO_TS\VTS_04_0.VOB
D:\VIDEO_TS\VTS_04_1.VOB
D:\VIDEO_TS\VTS_04_2.VOB
 
And this also worked:-

Call ListFiles("d:\", , True)
d:\VIDEO_TS\VIDEO_TS.BUP
d:\VIDEO_TS\VIDEO_TS.IFO
d:\VIDEO_TS\VIDEO_TS.VOB
d:\VIDEO_TS\VTS_01_0.BUP
d:\VIDEO_TS\VTS_01_0.IFO
d:\VIDEO_TS\VTS_01_0.VOB
d:\VIDEO_TS\VTS_01_1.VOB
d:\VIDEO_TS\VTS_01_2.VOB
d:\VIDEO_TS\VTS_01_3.VOB
d:\VIDEO_TS\VTS_01_4.VOB
d:\VIDEO_TS\VTS_01_5.VOB
d:\VIDEO_TS\VTS_01_6.VOB
d:\VIDEO_TS\VTS_02_0.BUP
d:\VIDEO_TS\VTS_02_0.IFO
d:\VIDEO_TS\VTS_02_0.VOB
d:\VIDEO_TS\VTS_02_1.VOB
d:\VIDEO_TS\VTS_03_0.BUP
d:\VIDEO_TS\VTS_03_0.IFO
d:\VIDEO_TS\VTS_03_0.VOB
d:\VIDEO_TS\VTS_03_1.VOB
d:\VIDEO_TS\VTS_04_0.BUP
d:\VIDEO_TS\VTS_04_0.IFO
d:\VIDEO_TS\VTS_04_0.VOB
d:\VIDEO_TS\VTS_04_1.VOB
d:\VIDEO_TS\VTS_04_2.VOB
 
But... for my C drive this took a long time:-

Call ListFiles("c:\", , True)

had the circle type hour glass displayed..... eventually got the following error:-

---------------------------
Microsoft Access
---------------------------
Error 52: Bad file name or number
---------------------------
OK
---------------------------

probably due to me trying to escape it....

I think it's 'cause of a large number of files... probably timing out somewhere....
 
thanks alot but is there some other code to get a list of files on a directory and sub directory?
 
Well, I would have thought Allen Browne's would be well thought out. Going by that, and without searching the internet which I'm sure you're capable of doing yourself, I would assume the problem isn't with the code, but it's with the speed that you can gain access to the file locations on the Windows system.

If you think about it Microsoft's own system for searching in Windows is very slow; try and find a file and you will know what I mean.

Having said that there are other products you can get which scan your hard drive quickly, I think they work by basically making a list of all of the documents and locations in advance, and then use this list to make a fast search utility.

I should mention that I am talking about something I know very little about! So my advice is to research it your self, and think of some fresh questions to ask on a forum, probably not this one, try a VBA forum, or possibly a Windows forum.

If you still can't find an off-the-shelf solution then you may be able to reconstruct Allen Browne's code so that instead of navigating masses of files/folders in one go, layer upon layer, you could adapt it to save a list of all the folder names; say start with the Drive root files, put the list in a table and then use that list to split your searches up into smaller searches avoiding the problem of a massive Drive search.
 
Last edited:
So my advice is to research it your self, and think of some fresh questions to ask on a forum, probably not this one, try a VBA forum, or possibly a Windows forum.
I thought this is a VBA forum Uncle G ;)

For this sort of thing nothing (in the VBA context) can compare to command prompt, so:

1. Shell() the following cmd to a file
Code:
dir "[COLOR="blue"]D:\Path\To\Root\Directory[/COLOR]" /A:-D /B /S > "[COLOR="Blue"]D:\Path\To\Destination\File.txt[/COLOR]"
... /S means recursively search subdirectories. Consult the help file to learn more about the other switches.

2. Read the contents from the file using FileSystemObject and display in your list
 
Ah! Yes, I meant VB6! If there are still any...
 
:D There must be a few still around. Lots of legacy systems still in use.

If you still can't find an off-the-shelf solution then you may be able to reconstruct Allen Browne's code so that instead of navigating masses of files/folders in one go, layer upon layer, you could adapt it to save a list of all the folder names; say start with the Drive root files, put the list in a table and then use that list to split your searches up into smaller searches avoiding the problem of a massive Drive search.
But david_3343434 I would go with Uncle Gizmo's advice above... listing the sub-directories instead of the files if there are too many files to list.
 

Users who are viewing this thread

Back
Top Bottom