Recursive directory search in VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blabello
    New Member
    • Dec 2007
    • 5

    Recursive directory search in VB.NET

    Good afternoon,

    I'm currently in the process of designing an application that downloads a zipped directory, extracts it, recurses through the directory while searching for any files. If a file is found, it is moved to a new folder. I've gotten the code that will move the files working, but I'm having a lot of trouble recursing through the directory. My inexperience and lack of knowledge with VB is not helping.

    My question, simply put, is how can I recurse through a directory and its subdirectories and search for any files they may contain?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    The Directory object will allow you to recurse.
    Code:
    string[] filenames = System.IO.Directory.GetFiles("path","search pattern", System.IO.SearchOption.AllDirectories)

    Comment

    • blabello
      New Member
      • Dec 2007
      • 5

      #3
      Originally posted by Plater
      The Directory object will allow you to recurse.
      Code:
      string[] filenames = System.IO.Directory.GetFiles("path","search pattern", System.IO.SearchOption.AllDirectories)

      Thanks for the prompt response. Could you kindly explain the function of the "search pattern" parameter?

      -B. Labello

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        It works just like the dir command in a command prompt.
        "*.txt" for all files that end in .txt
        "M*.*" all files that start with M
        etc
        etc

        Comment

        • blabello
          New Member
          • Dec 2007
          • 5

          #5
          Thanks for the help, Plater.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by Plater
            The Directory object will allow you to recurse.
            Code:
            string[] filenames = System.IO.Directory.GetFiles("path","search pattern", System.IO.SearchOption.AllDirectories)
            I'm curious whether this actually did resolve the issue. From another recent post, it seems as though GetFiles with the AllDirectories option doesn't give any indication of where the files were found, only their names. I suggested in the other thread that one could instead use GetDirectories to scan through the directories, and use a separate GetFiles for each, so you know where you are.

            I can't try it out, as I've only got VB6.

            Comment

            • balabaster
              Recognized Expert Contributor
              • Mar 2007
              • 798

              #7
              Originally posted by Killer42
              I'm curious whether this actually did resolve the issue. From another recent post, it seems as though GetFiles with the AllDirectories option doesn't give any indication of where the files were found, only their names.
              The strings returned are the full path names... so even though there was no confirmation, I'm confident it provided everything needed.

              A parse of my Inetpub\wwwroot folder with the all directories option selected returns an array with the following strings:

              C:\Inetpub\wwwr oot\Default.asp x
              C:\Inetpub\wwwr oot\OnCall\Defa ult.aspx
              C:\Inetpub\wwwr oot\OnCall\OnCa llEdit.aspx
              C:\Inetpub\wwwr oot\OnCall\OnCa llView.aspx
              C:\Inetpub\wwwr oot\OnCall\Pers onEdit.aspx
              C:\Inetpub\wwwr oot\OnCall\Admi n\RebuildHierar chyFromDB.aspx

              Hope that clarifies things

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by balabaster
                The strings returned are the full path names...
                Thanks for that, balabaster.

                That's really weird though. The problem encountered by the other person I mentioned was that it only provided the names with no paths, so they couldn't tell where the files came from. I admit, this did seem a surprising and pointless way for the function to operate. Perhaps it was a bug in some version that has since been fixed?

                The workaround that I suggested apparently worked, but now I have no idea why it was needed.

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  There is an alternate call that returns FileInfo[] and if you did not use the correct combination of properties from THAT object, you would only get the name of the file, and not the directory hierarchy.

                  Comment

                  Working...