473,412 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Searching ALL folders - recursion problem.

Markus
6,050 Expert 4TB
I'm trying to search through a specified file path (C:\\Users\<name>) and then delete specified file types. The code below works, but it only goes 1 sub-directory deep.

Does anyone know how I could modify this so that it searches every possible directory under the specified path.

Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args)
  2.         {
  3.             DirectoryInfo DirInfo = new DirectoryInfo(@"C:\\Users\Chris");
  4.  
  5.             GetDirs(DirInfo);
  6.         }
  7.  
  8.         public static void GetDirs(DirectoryInfo Dir)
  9.         {
  10.             DirectoryInfo[] Dirs = Dir.GetDirectories("*.*");
  11.             foreach( DirectoryInfo Directory in Dirs )
  12.             {
  13.                 Console.WriteLine(Directory.FullName);
  14.                 GetFiles(Directory);
  15.             }
  16.             Console.ReadLine();
  17.         }
  18.  
  19.         public static void GetFiles(DirectoryInfo Dir)
  20.         {
  21.             FileInfo[] Files;
  22.             try
  23.             {
  24.                 Files = Dir.GetFiles("*.*");
  25.                 foreach (FileInfo File in Files)
  26.                 {
  27.                     if (File.Extension == ".mp3")
  28.                     {
  29.                         Console.WriteLine("mp3 file found - " + File.Name + " in directory " + Dir.Name);
  30.                         Console.WriteLine("Deleting " + File.Name);
  31.                         File.Delete();
  32.                     }
  33.                 }
  34.             }
  35.             catch (Exception Ex)
  36.             {
  37.                 Console.WriteLine("Exception encountered: " + Ex.HelpLink);
  38.             }
  39.         }
  40.  
Nov 9 '08 #1
2 1061
mldisibio
190 Expert 100+
Add one line (cf line 16) to implement recursion:
Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args) 
  2.         { 
  3.             DirectoryInfo DirInfo = new DirectoryInfo(@"C:\\Users\Chris"); 
  4.  
  5.             GetDirs(DirInfo); 
  6.         } 
  7.  
  8.         public static void GetDirs(DirectoryInfo Dir) 
  9.         { 
  10.             DirectoryInfo[] Dirs = Dir.GetDirectories("*.*"); 
  11.             foreach( DirectoryInfo Directory in Dirs ) 
  12.             { 
  13.                 Console.WriteLine(Directory.FullName); 
  14.                 GetFiles(Directory); 
  15.                 // Implement Recursion:
  16.                 GetDirs(Directory);
  17.             } 
  18.             Console.ReadLine(); 
  19.         } 
  20.  
  21.         public static void GetFiles(DirectoryInfo Dir) 
  22.         { 
  23.             FileInfo[] Files; 
  24.             try 
  25.             { 
  26.                 Files = Dir.GetFiles("*.*"); 
  27.                 foreach (FileInfo File in Files) 
  28.                 { 
  29.                     if (File.Extension == ".mp3") 
  30.                     { 
  31.                         Console.WriteLine("mp3 file found - " + File.Name + " in directory " + Dir.Name); 
  32.                         Console.WriteLine("Deleting " + File.Name); 
  33.                         File.Delete(); 
  34.                     } 
  35.                 } 
  36.             } 
  37.             catch (Exception Ex) 
  38.             { 
  39.                 Console.WriteLine("Exception encountered: " + Ex.HelpLink); 
  40.             } 
  41.         } 
  42.  
P.S. It is good programming practice (adds clarity) to not use class names as local variables, or at least not with the same case. Using "File" and "Directory" as local variables can confuse someone who needs to read your code in the future.
Just a suggestion. - Mike
Nov 10 '08 #2
Curtis Rutland
3,256 Expert 2GB
Well, there's an even simpler way to do this.
Expand|Select|Wrap|Line Numbers
  1. DirectoryInfo dirInfo = new DirectoryInfo(@"C:\whateverpath\");
  2. FileInfo[] files = dirInfo.GetFiles("*.mp3", SearchOption.AllDirectories);
  3.  
That assumes that you are searching for .mp3 files. Now you have an array of all .mp3 files in a particular directory and it's children. Also, the FileInfo class has a property called Directory that will return a DirectoryInfo object pointing at the file's directory. You can really slim this program down. It does the recursion for you.

Hope that helps.

Also, your path is quite strange... the @ says to ignore all escape sequences except \" but you escape the first backslash, and not the second. That may not have the desired results...
Nov 10 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: megha | last post by:
Hi I want to create a tree type structure for my folders directory in java how i can do it. I know it will be a big programming but how to start please advice.
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
6
by: Joris De Groote | last post by:
Hi, I have a program that must look in in certain folder and take out every file (also the files in subfolders). Now it's no problem to do this with a few for's and if's. However, I don't know...
0
MMcCarthy
by: MMcCarthy | last post by:
This is a module that scans for files and folders on a specified path and describe them in comma separated values file in a text format. The information is stored in this file consecutively like:...
12
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an...
13
by: Mumia W. | last post by:
Hello all. I have a C++ program that can count the YOYOs that are in a grid of Y's and O's. For example, this Y O Y O O Y O Y O Y O O Y O Y Y O Y O Y O O Y O O Y Y O Y O
4
by: jodleren | last post by:
Hi! I wonder, which way to do this fastest. I have a disk, where I need to search for a file or directory. I do it recursively, meaning that I start from the top dir, then I add all...
30
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript?...
6
by: deve8ore | last post by:
Hello, We have a vendor that will supply us many files, and unfortunately will place them in different folders with no uniformity (within Windows Explorer). I'd like to have the capability to...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.