recursion - Recusively trying to find file within folder using keyword (VBA-Access) -


i creating vba-access application drop down box combo_history gives user ability launch .pdf file sub-folder within main folder called "scanned work orders (archives)". trying use number called "m" number(m number because every number starts m ex: m765196) find file without using specific sub folder here have far:


dim fso, ofolder, osubfolder, ofile, queue collection

set fso = createobject("scripting.filesystemobject") set queue = new collection queue.add fso.getfolder("t:\scanned work orders (archives)")   while queue.count > 0     set ofolder = queue(1)     queue.remove 1 'dequeue     if ofile = combo_history.value             application.followhyperlink ("t:\scanned work orders (archives)" & ofile)          end if     each osubfolder in ofolder.subfolders         queue.add osubfolder 'enqueue     next osubfolder     each ofile in ofolder.files         if ofile = combo_history.value             application.followhyperlink ("t:\scanned work orders (archives)" & ofile)          end if     next ofile loop 

the problem gets stuck in infinite loop because cannot find .pdf keyword name m765196 though in folder. there im missing? or easier way find .pdf file?

i'm adding second answer here because solving wildcard differed more anticipated original.

searching files using wildcard isn't difficult, comes implications, such returning list of results instead of single result. in addition, fortunately ran permissions error on 1 of subfolders caused me think how handle situation.

option explicit  private recursedepth integer  sub test()     dim rootfolder string     dim filename string     dim resultfiles() string     dim integer      rootfolder = "c:\temp"     filename = "*.pdf"      if findfiles(rootfolder, filename, resultfiles) > 0         = 1 ubound(resultfiles)             debug.print format(i, "00") & ": " & resultfiles(i)         next     else         debug.print "no files found!"     end if end sub  public function findfiles(thisfolder string, filespec string, _                           byref filelist() string) integer     '--- starts in given folder , checks files against filespec.     '    filespec may have wildcard specified, function returns     '    array of full pathnames (strings) each file matches     '      parameters:  thisfolder - string containing full path root     '                                folder search     '                   filespec   - string containing single filename     '                                search for, --or--     '                                string containing wildcard string of     '                                files search     '        (result==>)filelist   - array of strings, each full     '                                path file matching input filespec     '         returns:  (integer) count of files found match filespec     on error goto error_findfile     static fso object     static pathcollection collection     dim fullfilepath string     dim ofile object     dim ofolder object     dim osubfolder object      '--- first time through, set working objects     if recursedepth = 0         set fso = createobject("scripting.filesystemobject")         set pathcollection = new collection     end if     recursedepth = recursedepth + 1      '--- focus on given folder     set ofolder = fso.getfolder(thisfolder)      '--- first test if have permissions access folder ,     '    if there files in folder     on error resume next     if ofolder.files.count > 0         if err.number = 0             '--- loop through items in folder. files ,             '    folders -- use recursion search subfolders             each ofile in ofolder.files                 if ofile.name filespec                     pathcollection.add ofolder.path & "\" & ofile.name                 end if             next ofile             each osubfolder in ofolder.subfolders                 findfiles osubfolder.path, filespec, filelist             next osubfolder         else             '--- if here it's permissions error,             '    skip folder             err.clear         end if     end if     on error goto error_findfile  exit_findfile:     recursedepth = recursedepth - 1     if (recursedepth = 0) , (pathcollection.count > 0)         '--- pull paths out of collection , make array, because         '    programs uses arrays more         redim filelist(1 pathcollection.count)         dim integer         = 1 pathcollection.count             filelist(i) = pathcollection.item(i)         next     end if     findfiles = pathcollection.count     exit function  error_findfile:     debug.print "error (" & err.number & "): " & err.description & _                         " on " & osubfolder.path     goto exit_findfile  end function 

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -