Link to home
Start Free TrialLog in
Avatar of JoshinHtown
JoshinHtownFlag for United States of America

asked on

Search Directory and Sub Folders for Files that contain a certain string and store file paths in ArrayList

Hello All-

I'm in search of a C# function(s) that can search a directory and its sub folders for file(s) that contain a certain string and then store those file(s) full paths in a ArrayList.  So for example if I wanted to find all files that had "ABC123" in their filename and the starting directory was C:\Temp.  C\Temp has multiple subfolders.  Please let me know.  Regards,
ASKER CERTIFIED SOLUTION
Avatar of Reza Rad
Reza Rad
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you can do this for both files and directories:

string[] strFiles=System.IO.Directory.GetFiles(@"C:\temp","*ABC123*",System.IO.SearchOption.AllDirectories);
            string[] strDirectories = System.IO.Directory.GetDirectories(@"C:\temp", "*ABC123*", System.IO.SearchOption.AllDirectories);

Open in new window

Avatar of JoshinHtown

ASKER

Hello reza_rad-

when I used your code in your first response strFiles did not have any length when I ran it against my required parameters.  When I modified your code to use an ArrayList like below I at least got one count returned.  How can I modify your code below to find every file and add it to ArrayList?

Thanks,


string sMatch = "ABC123";

public void DirSearch(string sMatch)
{
    ArrayList matches = new ArrayList();
    matches.Add(Directory.GetFiles(@"C:\Temp", sMatch, SearchOption.AllDirectories));

    int count = matches.Count;
}
try the code I suggested, and use strFiles.Count
Directory.GetFiles or Directory.GetDirectories will return string array, so you should put the result in string[] variable, as I bring it before.
hmm this is strange.  I'm still not getting anything loaded into strFiles.  There are at least two files that should get picked up by this code.  Thoughts?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi jfputnam-

I tried your code as well and setup my parameters in the Main while making sure I had C:\\Temp.  I placed breakpoints in loadFilesFromDirs because I noticed it was entering the last Else suggesting it a non-directory or that it was skipping that fileName.  Inside C:\\Temp there is a folder called Arkansas that it skips but I cannot understand why.  The files I need returned are inside this folder.  The file pattern will vary.
Hi try the below code:

Here: 'PathofRootDir' simple means,as you mentioned: C:\temp\
i.e. 'temp' and all its subfolders.

and will return you an ArrayList of those files' path which you want to save. :)
public static ArrayList SearchAndSave(string Filename, string PathofRootDir)
        {
         ArrayList Al=new ArrayList(Directory.GetFiles(PathofRootDir, "*.*", SearchOption.AllDirectories));
         return Al;
        }

Open in new window

Oooops, I forgot to use the parameter Filename :O
Avatar of jfputnam
jfputnam

Does the same folder get excluded with all techiques you have tried?
Jfputman-

I see where your code is failing on my end.  Inside getFileListFromDirFiltered at the very bottom where your below foreach loop is.  Whenever it hits the first dir.GetFiles(useFileFilter) and then tries to jump to FileInfo f before entering loop it just fails.  useFileFilter is set to ABC123 which is what I'm looking for in any file name within these folders.  Two Files should get returned but none do through this code.  Any thoughts?

 foreach (FileInfo f in dir.GetFiles(useFileFilter))
                {
                    String name = f.Name;

                    //---accumulate file names

                    if (fullFileNames) filesBack.Add(dirPath + "\\" + name);
                    else filesBack.Add(name);
                }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Josh I just put in some files ABC123 in several sub directories and if found every one. Do you have file extents hidden. If so then maybe you want to use pattern "ABC123*" or "ABC123.*"
I ended up getting this working with the very first post from reza_rad.  I had left off the Stars on the search string, sorry reza.  Some of y'all had me going in the right direction as well as I was getting farther through your code so I will accept multiple solutions but reza got me finished.  Thank you everyone.
Josh if you have filename like:


>      2ABC2.txt
>      2ABC.txt

and you need ABC.txt you will need a mismatch in reza's post :)
really?  I just tested it and it seemed to return the two files I needed just fine and they had extra characters in them.  I will post another thread or try to get y'alls code working if reza's doesn't work but it seems to work
I will recommend you to use Regular expression for the matching of filenames. It will always give you the correct result.
You can use this regular expression as i posted:

            Regex re = new Regex("^" + Filename + "$");


-Shahan
Thanks Shahan-  I will definitely make notes of your regular expression and everyone elses comments on here.  I posted as many points as I could to give everyone points.  Thanks again everyone.
JoshinHtown, points doesn't matter, I just want you to go into positive direction.