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 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
                            • L Lost User

                              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 Offline
                              M Offline
                              michaelgr1
                              wrote on last edited by
                              #21

                              Hello, I tried many ways to do it (as you wrote and another ways) but in all of them it doesn't always find the file i need. For example if the starting path is the immediate folder that contains the file it works fine and finds the file, but when the starting patch is a higher (maybe 2-3 folder upper) it doesn't find this file (i write exactly the same name). It seems it doesn't enough time to the program to collect all the files names inside the folder. I did a little delay but it didn't help. This is my code: foreach (string d in Directory.GetDirectories(Start_dir)) { if (!d.Contains("Shared Documents")) { System.Threading.Thread.Sleep(200); foreach (string f in Directory.GetFiles(d)) { //MessageBox.Show(f); if (f.Contains(search)) { Process.Start(f); review_file_found = true; return; } } dirsearch(d, search); } }

                              M 1 Reply Last reply
                              0
                              • M michaelgr1

                                Hello, I tried many ways to do it (as you wrote and another ways) but in all of them it doesn't always find the file i need. For example if the starting path is the immediate folder that contains the file it works fine and finds the file, but when the starting patch is a higher (maybe 2-3 folder upper) it doesn't find this file (i write exactly the same name). It seems it doesn't enough time to the program to collect all the files names inside the folder. I did a little delay but it didn't help. This is my code: foreach (string d in Directory.GetDirectories(Start_dir)) { if (!d.Contains("Shared Documents")) { System.Threading.Thread.Sleep(200); foreach (string f in Directory.GetFiles(d)) { //MessageBox.Show(f); if (f.Contains(search)) { Process.Start(f); review_file_found = true; return; } } dirsearch(d, search); } }

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

                                Or it even seems as it doesn't have enough memory to do this search. Because files (in directories) that are located in the "beginning of the list" it finds (and opens), But files that are located a little "deeper" in the list he can't file and even the application get stucked in such situations

                                W 2 Replies Last reply
                                0
                                • M michaelgr1

                                  Or it even seems as it doesn't have enough memory to do this search. Because files (in directories) that are located in the "beginning of the list" it finds (and opens), But files that are located a little "deeper" in the list he can't file and even the application get stucked in such situations

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

                                  If there is way so i can send you the project just replace the path, recompile, run the project. put expression and click on the button. list will be displayed. It is working on my PC.

                                  M 1 Reply Last reply
                                  0
                                  • W WinSolution

                                    If there is way so i can send you the project just replace the path, recompile, run the project. put expression and click on the button. list will be displayed. It is working on my PC.

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

                                    OK, You can send me it to michgr@nana.co.il thanks very much

                                    W 1 Reply Last reply
                                    0
                                    • M michaelgr1

                                      Or it even seems as it doesn't have enough memory to do this search. Because files (in directories) that are located in the "beginning of the list" it finds (and opens), But files that are located a little "deeper" in the list he can't file and even the application get stucked in such situations

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

                                      here is it download it http://rapidshare.com/files/238592801/WindowsApplication1.rar.html[^]

                                      1 Reply Last reply
                                      0
                                      • M michaelgr1

                                        OK, You can send me it to michgr@nana.co.il thanks very much

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

                                        i have sent a mail also to you i was in hurry so without subject

                                        M 2 Replies Last reply
                                        0
                                        • W WinSolution

                                          i have sent a mail also to you i was in hurry so without subject

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

                                          OK thanks, I will try it and let you know

                                          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