473,480 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
1 2392

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
15618
by: Ben | last post by:
Greetings, I am looking for a way to search for and delete files based on a pattern mask. For example, the search method would find all files matching a certain pattern containing wildcards (e.g....
1
383
by: Earl Teigrob | last post by:
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...
2
1656
by: David | last post by:
How can I delete files at once? Like: " delete C:\MyFiles.* "
2
2131
by: Ken Yee | last post by:
First a little background: I've written an httphandler to handle wildcard extensions (i.e., I want to handle all URLs that come in rather than just URLs w/ a specific file extension so I can give...
3
10044
by: richardkreidl | last post by:
I have the following module that I delete old files based on how old they are: Sub Main() Dim First_Date As String = Date.Today.AddDays(-7) Dim Archive_Files() As String =...
6
1659
by: David Thielen | last post by:
Hi; I have tried both: <add verb="*" path="html-image\*.jpg" type="BitmapView"/> and <add verb="*" path="html-image\*" type="BitmapView"/> But neither works. In both cases I just get file...
11
5752
by: comp.lang.php | last post by:
Once again, I thought my class method deleteZip() would do the trick, but it never deletes any .zip* file found in a directory: /** * Delete any latent ZIP files found in this album. This...
1
2193
by: =?Utf-8?B?UGF1bA==?= | last post by:
I have a folder with the following files: a.doc a.csv a.pdf Sometimes all three files are there. Sometimes there is only one file there. Is there a File.Delete command that I can use that...
15
1394
by: Andy B | last post by:
I need to count files in a certain directory that has the string -Contract Template.xml at the end of it. How would I do this?
7
7887
by: Apros | last post by:
Okay, this is what I need to do, and I keep getting confused. I need to remove all files in a folder, then delete the folder, it's part of an installer I'm making, and it creates a directory, I just...
0
7037
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6895
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5326
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
176
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.