Algorythm to delete all files under a directory and subs based on wildcard

  • Thread starter Thread starter Earl Teigrob
  • Start date Start date
E

Earl Teigrob

Does someone have or know of an algorythm (method) that will delete all
files under a give directory and its subdirectories based on a wildcard
mask?

I can use this for one directory

for each file in Directory.GetFiles("pic1.*")
file.Delete()

but will need to use a recursive algorythm to get all its subdirectories

I thought I would try this news group before I went and wrote my own.

Thanks for your help

Earl
 
Earl,

The code below will help you scan a directory and it's subdirectorys
and let you check the files. You could implement the wildcard matches
with StartsWith, EndsWith and IndexOf depending on the number of
wldcards and thier position, or translate it to a regexpression.

public class DriveScanner
{
private DriveScanner()
{
}
public static void Scan(string drive)
{
DirectoryInfo dirInfo =
new DirectoryInfo(drive);
DriveScanner.Scan(dirInfo);
}

public static void Scan(DirectoryInfo dirInfo)
{
Console.WriteLine("Entering directory {0}", dirInfo.FullName);
FileInfo[] files = dirInfo.GetFiles();

foreach(FileInfo file in files)
{
Console.WriteLine(file.Name);
//
// Check filename and to your work
//
}

foreach(DirectoryInfo info in dirInfo.GetDirectories())
DriveScanner.Scan(info);
}
}
 
Back
Top