Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 26

Thread: FileSearch

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    FileSearch

    Finally got around to creating my own version. (Thanks to Bob and Ken whose code i pinched.)
    It's a little bit messy, so any suggestions to tidy it up?

    [vba]Option Explicit
    Option Compare Text

    Dim i As Long
    Dim LookingFor As String
    Dim FileSearch()


    Sub DoFileSearch()
    Dim FSO As Object
    Dim Pth As String
    Dim X As Long

    'Set options
    Pth = BrowseForFolder()

    'Pth = BrowseForFolder(MyDocuments)
    'Pth = BrowseForFolder("C:\") 'Processing C:\ can take a long time


    LookingFor = "" 'Process filter if required
    i = 0 'Counter if required
    Cells.Clear 'Clear old results if required

    ReDim FileSearch(6, 1000000)

    Set FSO = CreateObject("Scripting.FileSystemobject")
    Call ProcessFolder(FSO, Pth, True)
    If i = 0 Then
    MsgBox "No files found"
    Exit Sub
    Else
    ReDim Preserve FileSearch(6, i - 1)
    End If

    'Process found files
    Cells(1, 1).Resize(, 7) = Array("Path", "FileName", "Last Accessed", "Last Modified", "Created", "Type", "Size")
    Cells(2, 1).Resize(i, 7) = Application.Transpose(FileSearch)

    Set FSO = Nothing
    End Sub

    Private Function ProcessFolder( _
    ByRef FSO As Object, _
    ByVal Foldername As String, _
    Optional ByVal Init As Boolean)

    Dim Fldr As Object
    Dim SubFldr As Object
    Dim File As Object

    Set Fldr = FSO.GetFolder(Foldername)

    'Process head folder once only
    If Init = True Then
    For Each File In Fldr.Files
    ProcessFiles Fldr, File
    Next File
    End If

    On Error Resume Next
    For Each SubFldr In Fldr.SubFolders
    'Handle restricted folders e.g Recylce Bin
    If Not Err = 70 Then
    For Each File In SubFldr.Files
    ProcessFiles SubFldr, File
    Next File
    Call ProcessFolder(FSO, SubFldr.Path)
    End If
    Next SubFldr

    Set File = Nothing
    Set SubFldr = Nothing
    Set Fldr = Nothing
    End Function

    'Add files to array
    Sub ProcessFiles(Fld, f)
    If f.Name Like "*" & LookingFor Then
    FileSearch(0, i) = Fld.Path
    FileSearch(1, i) = f.Name
    FileSearch(2, i) = f.DateLastAccessed
    FileSearch(3, i) = f.DateLastModified
    FileSearch(4, i) = f.DateCreated
    FileSearch(5, i) = f.Type
    FileSearch(6, i) = f.Size

    i = i + 1
    End If
    End Sub

    Function MyDocuments() As String
    Dim wshShell As Object
    Set wshShell = CreateObject("WScript.Shell")
    MyDocuments = wshShell.Specialfolders("MyDocuments")
    Set wshShell = Nothing
    End Function

    Function BrowseForFolder(Optional OpenAt As Variant) As Variant

    'Function purpose: To Browser for a user selected folder.
    'If the "OpenAt" path is provided, open the browser at that directory
    'NOTE: If invalid, it will open at the Desktop level

    Dim ShellApp As Object

    'Create a file browser window at the default folder
    Set ShellApp = CreateObject("Shell.Application"). _
    BrowseForFolder(0, "Please choose a folder", 0, OpenAt)

    'Set the folder to that selected. (On error in case cancelled)
    On Error Resume Next
    BrowseForFolder = ShellApp.self.Path
    On Error GoTo 0

    'Destroy the Shell Application
    Set ShellApp = Nothing

    'Check for invalid or non-entries and send to the Invalid error
    'handler if found
    'Valid selections can begin L: (where L is a letter) or
    '\\ (as in \\servername\sharename. All others are invalid
    Select Case Mid(BrowseForFolder, 2, 1)
    Case Is = ":"
    If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
    Case Is = "\"
    If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
    Case Else
    GoTo Invalid
    End Select

    Exit Function

    Invalid:
    'If it was determined that the selection was invalid, set to False
    BrowseForFolder = False
    End Function

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not much you can do as I see it Malcolm, it all needs to be done.

    Personally, I would put it in a class, and see all the attributes as properties, such as Init etc.

    One question, why are you dropping the results into the worksheet in a loop, why not just dump the array on the range?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Bob,
    I'll have a look at a Class. The output was just a simple example of looping the return.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Were you thinking of coding some sort of browse for folder into it, as opposed to just My Documents? I agree about the class. It would allow more flexibility as to refine your search, perhaps by not only folder, but name, size, attributes, etc. Dunno, just thoughts.

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Good idea with the browser Zack. I'll add that in. Re the Class, I've never made use of them, so I'm doing some reading!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks Kenneth,
    I'll check it out tomorrow.
    I've reposted the code above with Ken's Browse routine and retrieved a bit more file data.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I disagree about the browser, or at least insofar as it is in the class. To my way of thinking, the class should be passed an initial folder, and work on that. If you want to browse that should be outside of the class routine.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Oh I would definitely make it outside the class, but I would still have that functionality. To me, if I were to use it, that would be one of my main desires. That and the functionality to refine my search in various ways. But I'm a user who loves options.

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Can't argue with that.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Latest (in progress) version which includes the original Sub verion along with my attempts at a Class (Thanks for the links Ken. Very instrucrive). For some reason the code "sticks" at different points for no apparent reasons.
    Advice gratefully received.

    The Browser code is used outwith the Class.
    Attached Files Attached Files
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    When browsing for folder, I use Application.FileDialog. e.g. http://www.vbaexpress.com/forum/showthread.php?t=24307

  13. #13
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Bombs when cancel is pressed.

    [vba] Set fldr = FSO.GetFolder(Foldername)[/vba]

    When "Foldername" returns "False".

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Zack,
    There are still a few bugs and things such as excluding System Folders I want to add, but I was stuck with the progress freezing.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  15. #15
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by mdmackillop
    ...For some reason the code "sticks" at different points for no apparent reasons. Advice gratefully received...
    Quote Originally Posted by mdmackillop
    Hi Zack,
    There are still a few bugs and things such as excluding System Folders I want to add, but I was stuck with the progress freezing.
    Hi Malcom,

    I am not sure we experienced the same thing, but it seemed that it would get "stuck" on a file for a bit and sometimes get going again, but sometimes just hang (but not properly break, if that makes sense). Anyways, a bit frustrating, as it seemed tough to get it to break and see where it was stuck.

    I think I found it :-)
    'Add files to array
    Sub ProcessFiles(Fld, f)
        If f.Name Like "*" & LookingFor Then
            i = i + 1
            FileSearch(1, i) = CStr(Fld.path)
            FileSearch(2, i) = CStr(f.Name)
            FileSearch(3, i) = CStr(f.DateLastAccessed)
            FileSearch(4, i) = CStr(f.DateLastModified)
            FileSearch(5, i) = CStr(f.DateCreated)
            FileSearch(6, i) = CStr(f.Type)
            FileSearch(7, i) = CStr(f.Size)
            'DoEvents
            Application.StatusBar = i & "-" & Fld.path & "\" & f.Name
        End If
    End Sub
    After 're-discovering' this, I recall that with the array being variant, it will try and sub-type the accessed/modified/created vals into dates. But every so often, the "Date/Time" will 'look' like a valid one, but the year will be some bizarre year like 1603 etc... Naturally it falls down.

    Anyways, I also changed FileSearch to a String, propbably not necessary but I think no harm. Since I was trying different stuff, I also changed it to a 1-based array, as you see I jump i's value before rather than after.

    Well, I hope that helps a little at least,

    Mark

  16. #16
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Mark,
    I had noticed issues with the dates and have tried CSTR etc. to change so simple strings. No improvement.
    I finally tried commenting out all the FileSearch(1,i) etc. lines, leaving an empty array. The code still "sticks" however.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  17. #17
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Good Morning Malcom,

    When you say "sticks", is it like my description? If just momentarily on certain files, are we talking about just where it is perceptible, or for several seconds or longer, or... does it actually hang?

    I pm'd you

    Mark

  18. #18
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Mark,
    On a restricted listing, such as MyDocuments, the statusbar stops but the code carries on and finally completes. If I search the full C:, it fails but becuse of the "sticking" number, I can't determine where!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  19. #19
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Hi Malcom,

    The hangs are the FSO system changing directories. But this is my opinion which is subject to change at any second for no apparent reason.

  20. #20
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by Tommy
    But this is my opinion which is subject to change at any second for no apparent reason.
    Like

Posting Permissions

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