user2577497
user2577497

Reputation: 497

Searching through directory with recursive function for txt files

I have a function that has a chosen folder and looks through for all files that are .txt extension.

The problem is that it will only search through and looks through the folders inside the chosen folder. It WON'T look at the .txt files that are in the chosen folders top layer, just the files in the sub-directories:

Chosen_Folder


test1.txt test2.txt Folder1 Folder2

So in other words, it ignores test1.txt, test2.txt, it will however find all .txt files in the folder and those folders sub-directories as well.

Here is my code:

public static void DirSearch(string sDir)
    {




        string ExtractedTXTBlocks = FINAL_PATH + "\\" + "Extracted TXT Blocks";
        System.IO.Directory.CreateDirectory(ExtractedTXTBlocks);

        try
        {
            foreach (string d in Directory.GetDirectories(sDir))
            {
                foreach (string f in Directory.GetFiles(d))
                {
                    string[] filelines = File.ReadAllLines(f);
                    string line = "";
                    string sBuilder = "";
                    int i = 1;
                    string temp = "";

                    string Fname = f.Substring(f.LastIndexOf("\\"), (f.Length - f.LastIndexOf("\\"))).Replace("\\","");
                    string Dname = d.Substring(d.LastIndexOf("\\"), (d.Length - d.LastIndexOf("\\"))).Replace("\\","");
                    StreamWriter sW = new StreamWriter(System.IO.Path.Combine(ExtractedTXTBlocks, Dname +"-" +Fname), true);


                    foreach (string item in filelines)
                    {
                        i++;
                        line = item;


                        line = (line.Replace("CMD", "")).Trim();
                        line = (line.Replace("0x", "")).Trim();
                        line = (line.Replace("0X", "")).Trim();
                        line = (line.Replace("DI", "")).Trim();
                        line = (line.Replace("SW", "")).Trim();
                        line = (line.Replace("LO 0", "")).Trim();
                        line = (line.Replace("LI", "")).Trim();
                        line = (line.Replace("LE 0", "")).Trim();
                        line = (line.Replace("REM", "")).Trim();
                        line = (line.Replace("$", "")).Trim();
                        line = (line.Replace("LO", "")).Trim();
                        line = (line.Replace("DO", "")).Trim();
                        line = (line.Replace(" ", "")).Trim();

                        if (line.StartsWith("1234") || line.StartsWith("1233"))
                        {

                            temp = line.Substring(8, line.Length - 8);
                            int number = int.Parse(temp);
                            temp = line.Substring(8, line.Length - 8).Replace(line.Substring(8, line.Length - 8), number.ToString("x").ToUpper());
                            line = line.Remove(8, line.Length - 8);
                            line += temp;
                            sBuilder = line;


                        }
                        else if (line.StartsWith("1235") || line.StartsWith("1255"))
                        {

                            temp = line.Substring(8, line.Length - 8);
                            int number = int.Parse(temp);                                
                            temp = line.Substring(8, line.Length - 8).Replace(line.Substring(8, line.Length - 8),number.ToString("x").ToUpper());
                            line = line.Remove(8, line.Length - 8);
                            line += temp;
                            sBuilder = line;


                        }

                        else if (line.Equals("Success"))
                        {

                            sW.WriteLine(sBuilder);                                
                            sBuilder = "";

                        }                            
                        else
                        {
                            if (!line.Equals(""))
                            {
                                sBuilder += line;
                            }


                        }





                    }
                    sW.Close();



                }
                DirSearch(d);
            }
        }
        catch (System.Exception excpt)
        {
            Console.WriteLine(excpt.Message);
        }
    }

I tried maybe setting the SearchOption to AllDirectories in the get folder method Directory.GetDirectories, but I think this is not what I need to do. I fear I am missing something very small.

Kind Regards.

Upvotes: 0

Views: 243

Answers (2)

user2577497
user2577497

Reputation: 497

Thanks for the help but I found another option here:

public static void DirSearch(string sDir)
    {




        string ExtractedTXTBlocks = FINAL_PATH + "\\" + "Extracted TXT Blocks";
        System.IO.Directory.CreateDirectory(ExtractedTXTBlocks);

        try
        {

                foreach (string f in Directory.GetFiles(sDir, "*.TXT", SearchOption.AllDirectories))
                {
                    string[] filelines = File.ReadAllLines(f);
                    string line = "";
                    string sBuilder = "";
                    int i = 1;
                    string temp = "";

                    string Fname = f.Substring(f.LastIndexOf("\\"), (f.Length - f.LastIndexOf("\\"))).Replace("\\","");
                    string Dname = Path.GetDirectoryName(f);
                    Dname = Dname.Substring(Dname.LastIndexOf("\\"), Dname.Length - Dname.LastIndexOf("\\")).Replace("\\", "");

                    StreamWriter sW = new StreamWriter(ExtractedTXTBlocks + "\\" + Dname + "-" + Fname, true);

                    foreach (string item in filelines)
                    {
                        i++;
                        line = item;


                        line = (line.Replace("CMD", "")).Trim();
                        line = (line.Replace("0x", "")).Trim();
                        line = (line.Replace("0X", "")).Trim();
                        line = (line.Replace("DI", "")).Trim();
                        line = (line.Replace("SW", "")).Trim();
                        line = (line.Replace("LO 0", "")).Trim();
                        line = (line.Replace("LI", "")).Trim();
                        line = (line.Replace("LE 0", "")).Trim();
                        line = (line.Replace("REM", "")).Trim();
                        line = (line.Replace("$", "")).Trim();
                        line = (line.Replace("LO", "")).Trim();
                        line = (line.Replace("DO", "")).Trim();
                        line = (line.Replace(" ", "")).Trim();

                        if (line.StartsWith("1234") || line.StartsWith("1233"))
                        {

                            temp = line.Substring(8, line.Length - 8);
                            int number = int.Parse(temp);
                            temp = line.Substring(8, line.Length - 8).Replace(line.Substring(8, line.Length - 8), number.ToString("x").ToUpper());
                            line = line.Remove(8, line.Length - 8);
                            line += temp;
                            sBuilder = line;


                        }
                        else if (line.StartsWith("1235") || line.StartsWith("1255"))
                        {

                            temp = line.Substring(8, line.Length - 8);
                            int number = int.Parse(temp);                                
                            temp = line.Substring(8, line.Length - 8).Replace(line.Substring(8, line.Length - 8),number.ToString("x").ToUpper());
                            line = line.Remove(8, line.Length - 8);
                            line += temp;
                            sBuilder = line;


                        }

                        else if (line.Equals("Success"))
                        {

                            sW.WriteLine(sBuilder);                                
                            sBuilder = "";

                        }                            
                        else
                        {
                            if (!line.Equals(""))
                            {
                                sBuilder += line;
                            }


                        }





                    }
                    sW.Close();



                }

        }
        catch (System.Exception excpt)
        {
            Console.WriteLine(excpt.Message);
        }
    }

This will just go through all files in the folder I say to look in, including the sub directories. I wanted to use the recursion scheme however it was not to well optimized for what I wanted to do.

Kind Regards.

Upvotes: 0

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Try This:

 Directory.GetFiles(d,"*.txt")

Upvotes: 1

Related Questions