Searching a folder for certain files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spazzo6281
    New Member
    • Feb 2008
    • 13

    Searching a folder for certain files

    Hello, is there a way to search a folder for certain files containing a wildcard or certain text entered into a text box. Right now all i can get is the whole list of files from a folder using the following code:

    lstbox.additems .addrange(IO.di rectory.getfile s("c:\"))

    How can i just return only files with matching text as whats in the text Box. Any help will be greatly appreciated. Thanks.

    Matt
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Originally posted by spazzo6281
    Hello, is there a way to search a folder for certain files containing a wildcard or certain text entered into a text box. Right now all i can get is the whole list of files from a folder using the following code:

    lstbox.additems .addrange(IO.di rectory.getfile s("c:\"))

    How can i just return only files with matching text as whats in the text Box. Any help will be greatly appreciated. Thanks.

    Matt
    Hello spazzo6281!

    Here is what I pulled up at microsoft:

    [CODE=VB]
    Dim MyFile, MyPath, MyName
    ' Returns "WIN.INI" if it exists.
    MyFile = Dir("C:\WINDOWS \WIN.INI")

    ' Returns filename with specified extension. If more than one *.ini
    ' file exists, the first file found is returned.
    MyFile = Dir("C:\WINDOWS \*.INI")

    ' Call Dir again without arguments to return the next *.INI file in the
    ' same directory.
    MyFile = Dir

    ' Return first *.TXT file with a set hidden attribute.
    MyFile = Dir("*.TXT", vbHidden)

    ' Display the names in C:\ that represent directories.
    MyPath = "c:\" ' Set the path.
    MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
    Do While MyName <> "" ' Start the loop.
    ' Ignore the current directory and the encompassing directory.
    If MyName <> "." And MyName <> ".." Then
    ' Use bitwise comparison to make sure MyName is a directory.
    If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
    Debug.Print MyName ' Display entry only if it
    End If ' it represents a directory.
    End If
    MyName = Dir ' Get next entry.
    Loop
    [/CODE]



    Try it, see if that works for ya;-)

    In a bit!

    Comment

    • spazzo6281
      New Member
      • Feb 2008
      • 13

      #3
      That didn't work, I'm just going to have to keep looking. Thanks for the help though.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        See here. The getfiles() method supports searching for files matching a wildcard specification.

        Comment

        • basti42
          New Member
          • Jun 2007
          • 18

          #5
          you can try this

          dirs = Directory.GetFi les("C:\\folder s", "*" + txtSeek.Text + "*" + ".csv");
          foreach (string dir in dirs)
          {
          listBox1.Items. Add(dir.Substri ng(34));
          strFound = dir;
          Directory.Creat eDirectory("C:\ \save_to);
          File.Copy(strFo und, "C:\\save_t o\\, true);
          }

          Comment

          • spazzo6281
            New Member
            • Feb 2008
            • 13

            #6
            I was finally able to get most of the code figured out with the following:

            Dim find As String = txtSearch.Text

            For Each foundfile As String In My.Computer.Fil eSystem.GetFile s("t:\documents ", FileIO.SearchOp tion.SearchAllS ubDirectories, find + "*.pdf", find + "*.doc")
            lstBox.Items.Ad d(foundfile)
            Next

            it works to find files that start with a given letter or the given first work but i won't find items if the word is second or third in the file name. if anyone can help me figure out how to do that I would greatly appreciate it. Thanks for that that helped on my first post.

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Just concatenate another "*" before find.

              Comment

              • spazzo6281
                New Member
                • Feb 2008
                • 13

                #8
                Originally posted by Killer42
                Just concatenate another "*" before find.
                Thanks, that worked.

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by spazzo6281
                  Thanks, that worked.
                  Excellent! :)

                  Glad we could help.

                  Comment

                  • spazzo6281
                    New Member
                    • Feb 2008
                    • 13

                    #10
                    Thanks to everyone that helped with this problem. Now that it's working all the found files come back like "t:\photos\*.jp g" does anybody out there know of a way to remove "t:\photos\ " and still have the "process.start( lstbox.text)" function work. I've tried everything that i could think of, and I guess I don't even know if it's possible. If anyone has any ideas I would appreciate it. Thanks.

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      This one should be relatively simple. Just have a look at the different properties available from the File object. I believe you'll find that one provides the name without the path. At present you're just using the default property, which apparently must be the full path.

                      Comment

                      • spazzo6281
                        New Member
                        • Feb 2008
                        • 13

                        #12
                        Originally posted by Killer42
                        This one should be relatively simple. Just have a look at the different properties available from the File object. I believe you'll find that one provides the name without the path. At present you're just using the default property, which apparently must be the full path.
                        I've looked and tried everything i could think of and it still doesn't work. I can use the trimstart feature but then the "process.start( lstbox.text)" doesn't work. I'm currently using "lstbox.items.a ddrange(IO.dire ctory.getfiles( "t:\photos" ))" to get the list of files but i still can't get path to know show up and have process.start still work. Thanks for trying.

                        Comment

                        • spazzo6281
                          New Member
                          • Feb 2008
                          • 13

                          #13
                          Originally posted by spazzo6281
                          I've looked and tried everything i could think of and it still doesn't work. I can use the trimstart feature but then the "process.start( lstbox.text)" doesn't work. I'm currently using "lstbox.items.a ddrange(IO.dire ctory.getfiles( "t:\photos" ))" to get the list of files but i still can't get path to know show up and have process.start still work. Thanks for trying.
                          The path to not show up. sorry for the miss type.

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            How about using the FileSystem.GetD irectories method to return the collection of directory names. Then for each of those, do a FileSystem.GetF iles and concatenate the directory name at the start of each returned filename.

                            Comment

                            • spazzo6281
                              New Member
                              • Feb 2008
                              • 13

                              #15
                              Originally posted by Killer42
                              How about using the FileSystem.GetD irectories method to return the collection of directory names. Then for each of those, do a FileSystem.GetF iles and concatenate the directory name at the start of each returned filename.
                              The program is finally doing what i wanted it to do. The concat method really helped. Thanks to everyone that helped me with my problems.

                              Comment

                              Working...