System.IO.File.Delete with Wildcards

I came across this earlier in the week, working on QualTrax code. In our application, we have a class for all our file handling operations. The class is pretty much a wrapper for standard System.IO.File calls (Copy, Delete, FileExists, etc). The wrapper class has been working quite well until this week, when I came across a need to delete a whole group of files. Without a second thought, I passed in wildcards in with my filename. For example:

C:\MyFolder\Prefix*.PDF

Lo and Behold, when I unsuspectingly ran the System.IO.File.Delete method, I got an error:

{"Illegal characters in path."}

It is fairly easy to workaround. The System.IO.Directory.GetFiles command does support wildcards in its searchPattern parameter. GetFiles returns an array of strings with the full paths to all the matching files. Instead of doing a straight File.Delete call, we want to get all the matching files, iterate through those and delete them.

There are a lot of examples floating around. However, all the sample code I found was in VB.NET, so I thought I'd go ahead and share C# code in case it is of value to anyone.

Note: You'll want to access the differences between System.IO.File/System.IO.FileInfo and System.IO.Directory/System.IO.DirectoryInfo and determine which one is best for your needs.

I kept the parameters to our wrapper function the same. We are still passing in a full path that includes our file name such as C:\MyFolder\Prefix*.PDF

In my wrapper function, I check our parameter (fullPath) for the presence of an astericks. If I find one, then I'm not going to do a straight File.Delete call.

Before I use the System.IO.Directory.GetFiles command I want to seperate out the directory path from the actual file name. So some quick substring action takes place.

Finally I call the System.IO.Directory.GetFiles command– passing in my filename (with the wildcards) as the second parameter.

If my array has no items– then there were no matching files. Hooray for that– we achieved the desired result without any work at all. Otherwise, we cycle through the array and now we call our friend File.Delete.

I surrounded the File.Delete calls with a try/catch clause so we can be meticulous with logging the details of the transaction (that code not pictured).

Thanks John. I’ve been trying to settle on a better means to communicate the code. Optimally, I’d like to use DIV tag with scrollbars, but it looks like the WordPress engine doesn’t quite approve of that notion!🙂

If you are looking for a good way to post your code there is an add in tool for Visual Studio 2005 that does a good job of converting source code to html while retaining all of the formatting and syntax highlighting. Once you install it you have a second copy function in the edit menu called Copy As HTML.

I am sending you proposal for affiliate marketing through your website to other advertisers. This is a performance based networking and we pay affiliate commissions on per lead or per sale basis. We are currently working for big brands in India for Retail, travel and Education sector. Few Major Brands are as below :

We wants to promote education advertisers through your website. We are offering good affiliate commissions on per sale and lead basis. Below are the details :
Merchant Category CPA CPL Status
Learnnext.com CBSE Courses 15% 30 Live
Toppers.com MBA course To be live
TopCatCoaching.com MBA course To be live
Career Launcher MBA course To be livehttp://www.meritnation.com
CBSE Courses To be live

Simpler way to handle wildcards: Don’t check for them. Here’s some code I was writing for deleting codes using params to allow multiple filemasks. It does make the assumption that if you don’t specify a directory, you are deleting from your current directory, so be careful:

It works because if the filemask you are supplying doesn’t have wildcards, it just makes Directory.GetFiles job easier by giving it a completely specified name. Either way, no need to check for wildcards.