Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2003
    Location
    London
    Posts
    341

    Question Unanswered: Directory's and Sub-Directory's

    Hi Guys,

    I'm just working on a database which captures file names from a directory. To do this I use the below code...

    The problem is, that I would like to Search the selected directory and all of the sub folders as well but I don't understand the code well enough to do this. Can anyone help?

    Christy


    Public Function ListDir(Path As String) As Long

    Dim ADOCon As ADODB.Connection
    Dim DirStr As String
    Dim DirCount As Long
    Dim SQL

    Set ADOCon = Application.CurrentProject.Connection

    DirCount = 0
    ListDir = 0
    DirStr = Dir(Path)

    If DirStr = "" Then Exit Function

    Do While DirStr <> ""

    DoCmd.SetWarnings False
    DoCmd.RunSQL "INSERT INTO Tbl_TempImages (Import, ImageLocation, ImageTitle) Values(1, '" & Path & "', '" & DirStr & "');"
    DoCmd.SetWarnings True
    DirCount = DirCount + 1
    DirStr = Dir

    Loop

    ListDir = DirCount

    End Function

  2. #2
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    try application.filesearch along the following lines:

    Code:
    private sub getFiles(strPath as string)
        dim i as integer
        with application.filesearch
            .newsearch
            .lookin = strPath           'this is the base path of your search
            .searchsubfolders = true
            .filename = "*.txt"         'use normal DOS wildcards convention
            if .execute() > 0 then
                msgbox .foundfiles.count & " file(s)", vbinformation, "found"
                for i = 1 to .foundfiles.count
                    'in real life, do something more useful
                    'than just dumping the names to debug
                    debug.print .foundfiles(i)
                next i
            else
                msgbox "zero files found!", vbexclamation, "not found"
            endif
        end with
    end sub
    izy
    currently using SS 2008R2

  3. #3
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Nice, succinct function Izy - I might adopt it in my prod db .
    I currently a use recursive fso function not unlike this one (don't have access to it now so can't post my own code). Minor advantage is you can also easily adapt to list empty subfolders too (if required). In case of interest - I use it in conjunction with Access automation to list all Access query names in a directory. Further adapted to search the SQL of the queries for key words.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  4. #4
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    pootle:
    take care with .filesearch!
    there are posts on the net that claim there are conflicts with Win indexing services (not a problem for me: indexing is off on all my machines as a religious principle, and i didn't need .filesearch in apps intended for other users' machines so far)
    ...your prod env will probably include folk with indexing on.

    Christy:
    for an approach that avoids fso & .filesearch, Chris Rae's stuff covers all the angles in enormous detail but will be slow (uses Dir & GetAttr)

    izy
    currently using SS 2008R2

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by izyrider
    pootle:
    take care with .filesearch!
    there are posts on the net that claim there are conflicts with Win indexing services (not a problem for me: indexing is off on all my machines as a religious principle, and i didn't need .filesearch in apps intended for other users' machines so far)
    ...your prod env will probably include folk with indexing on.
    Coo - thanks for the heads up Izy - I will look up. I suspect it will be all or nothing for me (only got it in prod internally). Ta as ever
    Testimonial:
    pootle flump
    ur codings are working excelent.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •