Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends. I have a folder in which there are 4-5 folders. I want to modify a file of name "kkj" in each folder. I don't know how to deal the sub folders in the main folder as an iteration. Please help me doing this.
Thank you so much
Posted

Please use this method System.IO.Directory.GetFiles(String, String, SearchOption)[^].

This will let you search a directory recursively. Works in .NET 2.0, 3.0, 3.5, 4.0.

Cheers!
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 13-Feb-11 12:42pm    
This is good, my 5.
I had to add important clarification about the problem which is often overlooked. It was discussed in couple of the past Questions. Please see my Answer.
--SA
This[^] little code snippet should surely help you.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Feb-11 12:42pm    
Good, my 5.
I had to add important clarification about the problem which is often overlooked. It was discussed in couple of the past Questions. Please see my Answer.
--SA
In addition the the Answers by Manfred and Abhinav: there is one difficulty related to how Microsoft treats the masks, it is counter-intuitive. Please see this discussion: Directory.Get.Files search pattern problem[^].

Pay attention to Where and ToLower in this code sample:

C#
var files = Directory.GetFiles(
    dir, "*.exe").
        Where(item => item.ToLower().EndsWith(".exe"));


(See the explanation if the references Question page.)

—SA
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 13-Feb-11 12:46pm    
Incredible! 5+
I followed up on your previous post. How did they (MS) ever get away with that. :)
Sergey Alexandrovich Kryukov 13-Feb-11 12:51pm    
Yes, there quite a few places where Microsoft decided to go with legacy and fails to deliver quality matching the rest of CLR. As the company quickly to the direction of making all fresh and correct, it seems it gain in saving time on development insignificantly compared to the costs of "putting it right... later"
--SA
Ali Al Omairi(Abu AlHassan) 13-Feb-11 16:44pm    
Sir, you are doing realy good. 5+

100 :rose: ;)
if u dont want to use the search pattern, u can write your own search algo and use it like this

C#
string path="D:\..... or what so ever u want ";
     if (Directory.Exists(path))
     {
         string[] dirs = Directory.GetDirectories(path);
         foreach (string dir in dirs)
         {
             string[] files = Directory.GetFiles(dir);
             foreach (string file in files)
             {
              if(File.Exists(file)
              {
              //Do the coding here
              }
          }
    }
 
Share this answer
 
Comments
Ali Al Omairi(Abu AlHassan) 13-Feb-11 16:46pm    
good alternative, sir.
dontumindit 13-Feb-11 16:48pm    
welcome bro, remember me in prayers
J imran 14-Feb-11 0:10am    
Thank you so much for the easy solution.
dontumindit 14-Feb-11 8:55am    
thanx bro, remeber me in your prayers

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900