Skip to content

Vb.net - recursive search for a file in all dir

tomcy thomas

Asked Feb 1, 2006, 10:56 PM EST1 answer
Share
LinkedInX

Hi,

May I get some help on this topic?
How to recursive search in dir (sub-dir) under a root directory (C:\)
For example
Search file pattern is *.txt
Root  dir is C:\
T
his program will search all sub dir in root and
If found add file name to the array list
In vb.net code
Thanks..

Tomcy

 

 

 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I tested in .Net Framework 3.5  C# windows application. it will search recursively in the entire drive. Just copy paste the code to a windows application and change the namespace to
    Code
    RecursiveSearch and add one button1 to the form.
    tomcy

    C#
     
    
    using System;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.IO;
    
    namespace RecursiveSearch
    {
        public partial class Form1 : Form
        {
            public string FilePattern = "*.sys";
            public string SearchPath = @"C:\";
            //public string SearchPath = @"C:\Documents and Settings\net11\Desktop\Russia";
            public string ListingFileName = @"c:\fileList.txt";
    
            ArrayList al = new ArrayList();
            public bool Recursive = true;
            StringBuilder sbErrorLogs = new StringBuilder();
            public string LogFileName = @"c:\fileLog.txt";
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
            #region Recursive method for to Search Fles
           
            private void RecursiveSearchingForFles(ref string strDirectory, ref ArrayList array, ref string DirPattern, bool Recursive)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(strDirectory);
                FileInfo[] pFileInfo = new FileInfo[0];
                Application.DoEvents();
                try
                {
                    pFileInfo = dirInfo.GetFiles(DirPattern); //getting all files from the folder
                }
                catch (UnauthorizedAccessException ex)
                {
                    //even this exception from any folder, the recusive program will move to next folder
                    //MessageBox.Show("Unauthorized Access to Folder/File!", ex.Message); return;
                    sbErrorLogs.AppendLine("from 1st catch : "+ex.Message + " : " + ex.Source);
                    return;
                }
                array.AddRange(pFileInfo);// to increase the capacity of array
                DirectoryInfo[] pdirInfo;
                pdirInfo = dirInfo.GetDirectories();
                if (Recursive == true)//if recursive is true only it will go for recursive else will take the files from the same folder
                {
                    Application.DoEvents();// this is to reduce system hanging
                    foreach (DirectoryInfo dirIter in pdirInfo)
                    {
                        strDirectory = dirIter.FullName;
                        RecursiveSearchingForFles(ref strDirectory, ref array, ref DirPattern, Recursive);
                    }
                }
            }
            #endregion
    
            private void button1_Click(object sender, EventArgs e)
            {
                sbErrorLogs.Length = 0;
                // the list of all files updated in the array called al.
                RecursiveSearchingForFles(ref SearchPath, ref al, ref FilePattern, Recursive);
                //label1.Text = "Total File count=" + al.Count.ToString();
                StringBuilder sb = new StringBuilder();
                foreach (FileInfo fn in al)
                {
                    sb.AppendLine(fn.FullName);
                }
                StreamWriter sw = new StreamWriter(ListingFileName);
                sw.Write(sb.ToString());
                sw.Close();
                sw.Dispose();
                StreamWriter LogFile = new StreamWriter(LogFileName);
                LogFile.Write(sbErrorLogs.ToString());
                LogFile.Close();
                LogFile.Dispose();
               
                MessageBox.Show("File Searching Over");
            }
        }
    }
    0