access vba - How to stop recursive search once the file is found in VBA -


i have below script , modify http://www.vbforums.com/showthread.php?613400-loop-through-folders-subfolders

private sub command1_click()  dim fld folder dim searchstring string dim resultfilepath string set fso = new filesystemobject set fld = fso.getfolder("c:\users\janedoe\desktop\jane")    searchstring = "claimsheet.xlsx"                         resultfilepath = recursivesearch(fld, searchstring)     set fld = nothing set fso = nothing  if resultfilepath = ""     msgbox ("we not find file " & searchstring) else     msgbox ("we found it, @ " & resultfilepath) end if  end sub  function recursivesearch(fld folder, search string) string dim tfold folder dim tfil file  each tfold in fld.subfolders     debug.print "looking in  " & tfold & " folder"     recursivesearch tfold, search      if recursivesearch = search         exit function     end if  next   debug.assert instr(tfil, search) = 0      if instr(tfil.name, search)                     recursivesearch = tfil.path         exit function     end if next   end function 

what recursivesearch function is, search folder searchstring file, once found, stop searching , return file path.

the problem is, can't exit function without losing value @ line

recursivesearch = tfil.path 

i think because function might have gone out of scope when returning upper level .

any appreciated,

your recursion line should be:

recursivesearch = recursivesearch(tfold, search) 

this allow each level of recursion pass it's result chain.

also, following line seems missing code in question:

for each tfil in fld.files 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -