Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. find files

find files

Scheduled Pinned Locked Moved C#
question
28 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    michaelgr1
    wrote on last edited by
    #1

    Hello, How i can search for a particular gif file (and open it) in a particular directory. I need the search to be recusive, i mean there are many folders in this directory and in each directory other folder and so on. I need the program to search in all those directories for this file. How can i do it?

    L M R 3 Replies Last reply
    0
    • M michaelgr1

      Hello, How i can search for a particular gif file (and open it) in a particular directory. I need the search to be recusive, i mean there are many folders in this directory and in each directory other folder and so on. I need the program to search in all those directories for this file. How can i do it?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      // c:\MyDir\ is your parent Directory ....
      string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.gif",
      SearchOption.AllDirectories);

      I know nothing , I know nothing ...

      M R 2 Replies Last reply
      0
      • M michaelgr1

        Hello, How i can search for a particular gif file (and open it) in a particular directory. I need the search to be recusive, i mean there are many folders in this directory and in each directory other folder and so on. I need the program to search in all those directories for this file. How can i do it?

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        michaelgr1 wrote:

        How can i do it?

        See here[^]

        It is a crappy thing, but it's life -^ Carlo Pallini

        1 Reply Last reply
        0
        • M michaelgr1

          Hello, How i can search for a particular gif file (and open it) in a particular directory. I need the search to be recusive, i mean there are many folders in this directory and in each directory other folder and so on. I need the program to search in all those directories for this file. How can i do it?

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #4

          well like you said, write a recursive function. Basic idea...

          using System.IO;

          string FindFile(string initDirectory, string fileToFind)
          {
          DirectoryInfo dir = new DirectoryInfo(initDirectory);
          foreach(FileInfo file in dir.GetFiles())
          {
          if(file.Name == fileToFind)
          return file.FullName;
          }
          foreach(DirectoryInfo subDir in dir.GetDirectories())
          {
          return FindFile(subDir.FullName, fileToFind);
          }
          return null;
          }

          ..that should do the trick, if not I hope you get the idea

          Life goes very fast. Tomorrow, today is already yesterday.

          modified on Friday, May 29, 2009 9:03 AM

          M 1 Reply Last reply
          0
          • L Lost User

            // c:\MyDir\ is your parent Directory ....
            string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.gif",
            SearchOption.AllDirectories);

            I know nothing , I know nothing ...

            M Offline
            M Offline
            musefan
            wrote on last edited by
            #5

            lol... bit too easy for my liking ;)

            Life goes very fast. Tomorrow, today is already yesterday.

            1 Reply Last reply
            0
            • M musefan

              well like you said, write a recursive function. Basic idea...

              using System.IO;

              string FindFile(string initDirectory, string fileToFind)
              {
              DirectoryInfo dir = new DirectoryInfo(initDirectory);
              foreach(FileInfo file in dir.GetFiles())
              {
              if(file.Name == fileToFind)
              return file.FullName;
              }
              foreach(DirectoryInfo subDir in dir.GetDirectories())
              {
              return FindFile(subDir.FullName, fileToFind);
              }
              return null;
              }

              ..that should do the trick, if not I hope you get the idea

              Life goes very fast. Tomorrow, today is already yesterday.

              modified on Friday, May 29, 2009 9:03 AM

              M Offline
              M Offline
              michaelgr1
              wrote on last edited by
              #6

              Hello, This code gives me the following errors: Error Cannot convert to static type 'System.IO.Directory' Error Cannot convert type 'System.IO.DirectoryInfo' to 'System.IO.Directory' Error The name 'FindFile' does not exist in the current context Error 'System.IO.Directory' does not contain a definition for 'Fullpath'

              M 1 Reply Last reply
              0
              • M michaelgr1

                Hello, This code gives me the following errors: Error Cannot convert to static type 'System.IO.Directory' Error Cannot convert type 'System.IO.DirectoryInfo' to 'System.IO.Directory' Error The name 'FindFile' does not exist in the current context Error 'System.IO.Directory' does not contain a definition for 'Fullpath'

                M Offline
                M Offline
                musefan
                wrote on last edited by
                #7

                see edited code... thou you should have been able to fix those mistakes yourself. The error messages are very straight forward.

                Life goes very fast. Tomorrow, today is already yesterday.

                M 1 Reply Last reply
                0
                • M musefan

                  see edited code... thou you should have been able to fix those mistakes yourself. The error messages are very straight forward.

                  Life goes very fast. Tomorrow, today is already yesterday.

                  M Offline
                  M Offline
                  michaelgr1
                  wrote on last edited by
                  #8

                  I'm sorry, I fixed those errors myself , but it still doesn't work properly as i need. it enters only the subdirectories of the first directory (in the initdirectory) and so on. If i have many directories in the main directory (initdirectory) and so on in the other subdirectories it doesn't work. Of course if the file is located somewhre there in the subdirectories of the subdirectories...

                  M 1 Reply Last reply
                  0
                  • L Lost User

                    // c:\MyDir\ is your parent Directory ....
                    string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.gif",
                    SearchOption.AllDirectories);

                    I know nothing , I know nothing ...

                    R Offline
                    R Offline
                    Rob Philpott
                    wrote on last edited by
                    #9

                    Stark DaFixzer wrote:

                    SearchOption.AllDirectories

                    Not seen that before and have always written a recursive procedure for this sort of thing. Does this work?

                    Regards, Rob Philpott.

                    B L M 3 Replies Last reply
                    0
                    • M michaelgr1

                      I'm sorry, I fixed those errors myself , but it still doesn't work properly as i need. it enters only the subdirectories of the first directory (in the initdirectory) and so on. If i have many directories in the main directory (initdirectory) and so on in the other subdirectories it doesn't work. Of course if the file is located somewhre there in the subdirectories of the subdirectories...

                      M Offline
                      M Offline
                      musefan
                      wrote on last edited by
                      #10

                      hmmm.. post the code you are using

                      Life goes very fast. Tomorrow, today is already yesterday.

                      M 1 Reply Last reply
                      0
                      • M musefan

                        hmmm.. post the code you are using

                        Life goes very fast. Tomorrow, today is already yesterday.

                        M Offline
                        M Offline
                        michaelgr1
                        wrote on last edited by
                        #11

                        I used exactly the code you wrote. but it doesn't searches in the directories as i need (as i wrote in the previous post)

                        M 1 Reply Last reply
                        0
                        • M michaelgr1

                          I used exactly the code you wrote. but it doesn't searches in the directories as i need (as i wrote in the previous post)

                          M Offline
                          M Offline
                          michaelgr1
                          wrote on last edited by
                          #12

                          Also i tries this way- private void dirsearch(string Start_dir, string search) { MessageBox.Show(search); try { foreach (string d in Directory.GetDirectories(Start_dir)) { MessageBox.Show(d); foreach (string f in Directory.GetFiles(Start_dir)) { if (f.Contains(search)) { if (!f.Contains(".doc")) Process.Start(f); review_file_found = true; } } dirsearch(d, search); } } catch (System.Exception excpt) { Console.WriteLine(excpt.Message); } } But it doesn't find the file as well (but seems that search ok- in the sub-directories)

                          M 1 Reply Last reply
                          0
                          • M michaelgr1

                            Also i tries this way- private void dirsearch(string Start_dir, string search) { MessageBox.Show(search); try { foreach (string d in Directory.GetDirectories(Start_dir)) { MessageBox.Show(d); foreach (string f in Directory.GetFiles(Start_dir)) { if (f.Contains(search)) { if (!f.Contains(".doc")) Process.Start(f); review_file_found = true; } } dirsearch(d, search); } } catch (System.Exception excpt) { Console.WriteLine(excpt.Message); } } But it doesn't find the file as well (but seems that search ok- in the sub-directories)

                            M Offline
                            M Offline
                            musefan
                            wrote on last edited by
                            #13

                            what string are you passing in as the file to find?

                            Life goes very fast. Tomorrow, today is already yesterday.

                            M 1 Reply Last reply
                            0
                            • R Rob Philpott

                              Stark DaFixzer wrote:

                              SearchOption.AllDirectories

                              Not seen that before and have always written a recursive procedure for this sort of thing. Does this work?

                              Regards, Rob Philpott.

                              B Offline
                              B Offline
                              Baeltazor
                              wrote on last edited by
                              #14

                              I agree with musefan LOL... but yep, it does work :D

                              1 Reply Last reply
                              0
                              • M musefan

                                what string are you passing in as the file to find?

                                Life goes very fast. Tomorrow, today is already yesterday.

                                M Offline
                                M Offline
                                michaelgr1
                                wrote on last edited by
                                #15

                                for example; 061_pmr_c0_txhb_cut_rxhb_to_smx.vl.GIF

                                W L 2 Replies Last reply
                                0
                                • R Rob Philpott

                                  Stark DaFixzer wrote:

                                  SearchOption.AllDirectories

                                  Not seen that before and have always written a recursive procedure for this sort of thing. Does this work?

                                  Regards, Rob Philpott.

                                  L Offline
                                  L Offline
                                  Luc Pattyn
                                  wrote on last edited by
                                  #16

                                  Yes it works. There are some disadvantages: it will search all before it returns anything. (And the results may be huge). .NET 4.0 will offer some new methods, enumerating while searching, rather than building an array. And a limitation: it only works well as long as a single filter is all you need. Finding all BMP, GIF, PNG, JPEG would need many of those (resulting in a different order), or another tactic. :)

                                  Luc Pattyn [Forum Guidelines] [My Articles]


                                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                                  L 1 Reply Last reply
                                  0
                                  • M michaelgr1

                                    for example; 061_pmr_c0_txhb_cut_rxhb_to_smx.vl.GIF

                                    W Offline
                                    W Offline
                                    WinSolution
                                    wrote on last edited by
                                    #17

                                    Just open visual studio 2005 / latest add one button (to intiate click event ), one textbox (contains expression you want to search ), and one listbox to show the results add click event on Button1 by double clicking on it and paste following code //replace mine path with your own DirectoryInfo DirInfo = new DirectoryInfo(@"F:\Documents and Settings\Administrator\My Documents\"); FileInfo[] DirFiles = DirInfo.GetFiles(textBox1.Text, SearchOption.AllDirectories); listBox1.Items.Clear(); foreach (FileInfo FileObj in DirFiles) { listBox1.Items.Add(FileObj); } goto top of file and add a namespace >> using System.IO; press F5 and run insert expression to search. click on button. result will be displayed to list box. if this doesnot work the provide me email address i will mail my project to you

                                    1 Reply Last reply
                                    0
                                    • L Luc Pattyn

                                      Yes it works. There are some disadvantages: it will search all before it returns anything. (And the results may be huge). .NET 4.0 will offer some new methods, enumerating while searching, rather than building an array. And a limitation: it only works well as long as a single filter is all you need. Finding all BMP, GIF, PNG, JPEG would need many of those (resulting in a different order), or another tactic. :)

                                      Luc Pattyn [Forum Guidelines] [My Articles]


                                      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #18

                                      Luc Pattyn wrote:

                                      Yes it works.There are some disadvantages: it will search all before it returns anything.

                                      Yes that is right !! :)

                                      I know nothing , I know nothing ...

                                      1 Reply Last reply
                                      0
                                      • M michaelgr1

                                        for example; 061_pmr_c0_txhb_cut_rxhb_to_smx.vl.GIF

                                        L Offline
                                        L Offline
                                        Lost User
                                        wrote on last edited by
                                        #19

                                        Why you didn't use my code ? (The first Comment ?) :confused: No offense , it's just a kind of discussion ... :rose: thank you ...

                                        I know nothing , I know nothing ...

                                        M 1 Reply Last reply
                                        0
                                        • R Rob Philpott

                                          Stark DaFixzer wrote:

                                          SearchOption.AllDirectories

                                          Not seen that before and have always written a recursive procedure for this sort of thing. Does this work?

                                          Regards, Rob Philpott.

                                          M Offline
                                          M Offline
                                          Mike Devenney
                                          wrote on last edited by
                                          #20

                                          Yep. I use it in a homebaked app to catalog my picture collection.

                                          Mike Devenney

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups