By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,414 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,414 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

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);
}
}
"Earl Teigrob" <ea******@hotmail.com> skrev i meddelandet
news:%2******************@TK2MSFTNGP12.phx.gbl...
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

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.