29

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

For example, delete all existing files in "C:\Folder" whose file name contains the keyword "Apple".

tshepang
  • 12,111
  • 21
  • 91
  • 136
user
  • 16,429
  • 28
  • 80
  • 97

5 Answers5

60

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
  • 56,972
  • 11
  • 108
  • 137
8

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");
H H
  • 263,252
  • 30
  • 330
  • 514
  • 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. – user Oct 25 '09 at 08:39
  • Then you can skip the ReadAllText/TextReader steps. – H H Oct 25 '09 at 08:40
  • 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. – Ilya Khaprov Oct 25 '09 at 08:40
  • Trickster, see the TextReader part. – H H Oct 25 '09 at 08:41
  • @Nate: the `System.IO.Directory.GetFiles` method that Henk suggests in his answer gives you the filenames as a string array. – Fredrik Mörk Oct 25 '09 at 09:08
7
new List<string>(Directory.GetFiles(@"C:\Folder")).ForEach(file => {
    if (file.IndexOf("apple", StringComparison.OrdinalIgnoreCase) >= 0)
        File.Delete(file);
});

or

new List<string>(Directory.GetFiles(@"C:\Folder")).ForEach(file => {
    Regex re = new Regex("apple", RegexOptions.IgnoreCase);
    if (re.IsMatch(file))
        File.Delete(file);
});
hammar
  • 138,522
  • 17
  • 304
  • 385
Keith
  • 71
  • 1
  • 2
5

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 Rosendo
  • 25,001
  • 7
  • 80
  • 118
0
new List<string>(Directory.GetFiles(@"C:\Folder")).ForEach(file => { if (file.ToUpper().Contains("apple".ToUpper())) File.Delete(file); });
Antony Koch
  • 2,043
  • 1
  • 16
  • 23