Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listing Files in Sub-Directory

Status
Not open for further replies.

EzehM

Programmer
Feb 27, 2003
86
GB
Please help.

I will like to list all the files in various subdirectories that match a certain criteria.

As an example.

I have the following directory structure

a) C:\abb\one-two-abb-source\sent
b) C:\abc\three-four-abc-source\sent
c) C:\abd\one-two-abd-source\sent
d) C:\abb\four-two-abb-source\sent
e) C:\aaa\four-two-aaa-source\sent
f) C:\aaa\four-three-aaa-source\sent

In this example, I am interested in the subdirectories that have abb & aaa as part of the directory name (in this case a ,d,e & f)

I want to list all files in them that have a .txt extension

i.e.
C:\abb\one-two-abb-source\sent, C:\aaa\four-two-aaa-source\sent, C:\aaa\four-three-aaa-source\sent & C:\abb\four-two-abb-source\sent

Any ideas please on how I can do this.

Thanks






 
This is a possible solution

Code:
private void btnSearchForFiles_Click(object sender, EventArgs e)
{
    [COLOR=green]// Clear the listbox:[/color green]
    lsbFiles.Items.Clear();

    System.Text.RegularExpressions.Regex expression = new System.Text.RegularExpressions.Regex("aaa|abb");

    [COLOR=green]// First search for files with an .txt extension[/color green]
    foreach (string file in Directory.GetFiles(@"C:\Temp", "*.txt", SearchOption.AllDirectories))
    {
        [COLOR=green]// Search for files that contain abb or aaa[/color green]
        if (expression.IsMatch(file))
        {
            lsbFiles.Items.Add(file);
        }
    }
}

Greet,

Geert Verhoeven
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top