Recursive File Search Using the Dir() Function Announcing the arrival of Valued Associate...

How discoverable are IPv6 addresses and AAAA names by potential attackers?

Overriding an object in memory with placement new

Why didn't this character "real die" when they blew their stack out in Altered Carbon?

String `!23` is replaced with `docker` in command line

How to run gsettings for another user Ubuntu 18.04.2 LTS

How to call a function with default parameter through a pointer to function that is the return of another function?

Storing hydrofluoric acid before the invention of plastics

How to deal with a team lead who never gives me credit?

Short Story with Cinderella as a Voo-doo Witch

Why are there no cargo aircraft with "flying wing" design?

2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?

What does "fit" mean in this sentence?

What exactly is a "Meth" in Altered Carbon?

What LEGO pieces have "real-world" functionality?

Why aren't air breathing engines used as small first stages

Fundamental Solution of the Pell Equation

Check which numbers satisfy the condition [A*B*C = A! + B! + C!]

How to find out what spells would be useless to a blind NPC spellcaster?

Why did the rest of the Eastern Bloc not invade Yugoslavia?

What causes the vertical darker bands in my photo?

Should I discuss the type of campaign with my players?

How does the particle を relate to the verb 行く in the structure「A を + B に行く」?

What's the meaning of 間時肆拾貳 at a car parking sign

porting install scripts : can rpm replace apt?



Recursive File Search Using the Dir() Function



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0












$begingroup$


There are plenty of examples of using the FileSystemObject to recursively search for a file in subfolders. I thought that it would be interesting to write one using the Dir() function.



I was wondering if there is a way to return the found file without using the extra FoundFile parameter?





Function FindFile(ByVal folderName As String, ByVal FileName As String, Optional ByRef FoundFile As String) As String
Dim search As String
Dim dirList As New Collection

If Not Right(folderName, 1) = "" Then folderName = folderName & ""
search = Dir(folderName & "*", vbDirectory)
While Len(search) > 0
If Not search = "." And Not search = ".." Then
If GetAttr(folderName & search) = 16 Then
dirList.Add folderName & search
Else
If LCase(search) = LCase(FileName) Then
FoundFile = folderName & FileName
FindFile = FoundFile
Exit Function
End If
End If

End If
search = Dir()
Wend

Dim fld
For Each fld In dirList
If Len(FoundFile) > 0 Then
FindFile = FoundFile
Exit Function
Else
FindFile = FindFile(CStr(fld), FileName, FoundFile)
End If
Next

End Function








share









$endgroup$



















    0












    $begingroup$


    There are plenty of examples of using the FileSystemObject to recursively search for a file in subfolders. I thought that it would be interesting to write one using the Dir() function.



    I was wondering if there is a way to return the found file without using the extra FoundFile parameter?





    Function FindFile(ByVal folderName As String, ByVal FileName As String, Optional ByRef FoundFile As String) As String
    Dim search As String
    Dim dirList As New Collection

    If Not Right(folderName, 1) = "" Then folderName = folderName & ""
    search = Dir(folderName & "*", vbDirectory)
    While Len(search) > 0
    If Not search = "." And Not search = ".." Then
    If GetAttr(folderName & search) = 16 Then
    dirList.Add folderName & search
    Else
    If LCase(search) = LCase(FileName) Then
    FoundFile = folderName & FileName
    FindFile = FoundFile
    Exit Function
    End If
    End If

    End If
    search = Dir()
    Wend

    Dim fld
    For Each fld In dirList
    If Len(FoundFile) > 0 Then
    FindFile = FoundFile
    Exit Function
    Else
    FindFile = FindFile(CStr(fld), FileName, FoundFile)
    End If
    Next

    End Function








    share









    $endgroup$















      0












      0








      0





      $begingroup$


      There are plenty of examples of using the FileSystemObject to recursively search for a file in subfolders. I thought that it would be interesting to write one using the Dir() function.



      I was wondering if there is a way to return the found file without using the extra FoundFile parameter?





      Function FindFile(ByVal folderName As String, ByVal FileName As String, Optional ByRef FoundFile As String) As String
      Dim search As String
      Dim dirList As New Collection

      If Not Right(folderName, 1) = "" Then folderName = folderName & ""
      search = Dir(folderName & "*", vbDirectory)
      While Len(search) > 0
      If Not search = "." And Not search = ".." Then
      If GetAttr(folderName & search) = 16 Then
      dirList.Add folderName & search
      Else
      If LCase(search) = LCase(FileName) Then
      FoundFile = folderName & FileName
      FindFile = FoundFile
      Exit Function
      End If
      End If

      End If
      search = Dir()
      Wend

      Dim fld
      For Each fld In dirList
      If Len(FoundFile) > 0 Then
      FindFile = FoundFile
      Exit Function
      Else
      FindFile = FindFile(CStr(fld), FileName, FoundFile)
      End If
      Next

      End Function








      share









      $endgroup$




      There are plenty of examples of using the FileSystemObject to recursively search for a file in subfolders. I thought that it would be interesting to write one using the Dir() function.



      I was wondering if there is a way to return the found file without using the extra FoundFile parameter?





      Function FindFile(ByVal folderName As String, ByVal FileName As String, Optional ByRef FoundFile As String) As String
      Dim search As String
      Dim dirList As New Collection

      If Not Right(folderName, 1) = "" Then folderName = folderName & ""
      search = Dir(folderName & "*", vbDirectory)
      While Len(search) > 0
      If Not search = "." And Not search = ".." Then
      If GetAttr(folderName & search) = 16 Then
      dirList.Add folderName & search
      Else
      If LCase(search) = LCase(FileName) Then
      FoundFile = folderName & FileName
      FindFile = FoundFile
      Exit Function
      End If
      End If

      End If
      search = Dir()
      Wend

      Dim fld
      For Each fld In dirList
      If Len(FoundFile) > 0 Then
      FindFile = FoundFile
      Exit Function
      Else
      FindFile = FindFile(CStr(fld), FileName, FoundFile)
      End If
      Next

      End Function






      vba





      share












      share










      share



      share










      asked 3 mins ago









      TinManTinMan

      1,2041110




      1,2041110






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217591%2frecursive-file-search-using-the-dir-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217591%2frecursive-file-search-using-the-dir-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

          How do i solve the “ No module named 'mlxtend' ” issue on Jupyter?

          Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...