views:

1439

answers:

4

I need to check a directory to see if there are any files whos file name contains a specific keyword and if there are, to delete them. Is this possible?

Example:

If there are any files in "C:\Folder" whos file name contains the keyword "Apple", delete them.

Thanks.

+4  A: 

You can use System.IO.Directory.GetFiles() to a list of the files, in string[] format.

Then you can use System.IO.File.ReadAllText() to read complete files, or if they are very big, open a TextReader with System.IO.File.OpenText().

If you are looking for a literal keyword, String.Contains() is all you need.

Deleting a file can be done with System.IO.File.Delete(). Make sure the file is closed again.

Edit, 2 examples of GetFiles():

string[] fileNames = System.IO.Directory.GetFiles(@"C:\");
string[] fileNames = System.IO.Directory.GetFiles(@"C:\", @"*.sys");
Henk Holterman
Whoops, guess I should have worded it a bit different. I don't need to check within the file itself, but rather just the file name.
Nate Shoffner
Then you can skip the ReadAllText/TextReader steps.
Henk Holterman
There is no need to read all text. E.g. if File starts with Apple and it is 50mb... The better way is to read files sequentially with slide window.
Trickster
Trickster, see the TextReader part.
Henk Holterman
How do I list all the files in string[] format?
Nate Shoffner
@Nate: the `System.IO.Directory.GetFiles` method that Henk suggests in his answer gives you the filenames as a string array.
Fredrik Mörk
+3  A: 

To expand on Henk's answer, you need:

string rootFolderPath = @"C:\\SomeFolder\\AnotherFolder\\FolderCOntainingThingsToDelete";
string filesToDelete = @"*DeleteMe*.doc";   // Only delete DOC files containing "DeleteMe" in their filenames
string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToDelete);
foreach(string file in fileList)
{
    System.Diagnostics.Debug.WriteLine(file + "will be deleted");
//  System.IO.File.Delete(file);
}

BE VERY CAREFUL!

Note that I've commented out the delete command. Run it and test it carefully before you let it actually delete anything!

If you wish to recursively delete files in ALL subfolders of the root folder, add ,System.IO.SearchOption.AllDirectories); to the GetFiles call.

If you do this it is also a very good idea to refuse to run if the rootFolderPath is less than about 4 characters long (a simple protection against deleting everything in C:\ - I've been there and done that and it's not fun!!!)

Jason Williams
And don't forget to catch exceptions, to handle situations where the file is in use, or access is denied, etc.
Bryan
Yeah, that bit I left as an exercise for the reader ;-)
Jason Williams
Thank you. Added the catch exceptions as well.
Nate Shoffner
+2  A: 

More or less, this:

string DeleteThis = "apple";
string[] Files = Directory.GetFiles(@"C:\Folder");

foreach (string file in Files)
{
    if (file.ToUpper().Contains(DeleteThis.ToUpper()))
    {
        File.Delete(file);
    }
}
Kyle Rozendo
A: 
new List<string>(Directory.GetFiles(@"C:\Folder")).ForEach(file => { if (file.ToUpper().Contains("apple".ToUpper())) File.Delete(file); });
Antony Koch