473,395 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2385

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
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
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
by: David | last post by:
How can I delete files at once? Like: " delete C:\MyFiles.* "
2
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
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
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
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
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
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
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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
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
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,...

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.