Home > Mobile >  Delete Files containing specific Text in Directory and Subdirectories
Delete Files containing specific Text in Directory and Subdirectories

Time:10-18

How to delete Files there names containing a specific string in a Directory and also all Subdirectories?

Given Filenames like:

EA myown EURJPY M15 3015494.mq5

EA myown EURJPY M15 3015494.ex5

EA self EURJPY M15 3098111 fine.mq5

EA self EURJPY M15 3098111 fine.ex5

Given Folderstructures like:

D:\TEMP\MYTEST

D:\TEMP\MYTEST\EURJPY

D:\TEMP\MYTEST\EURJPY\EURJPY_M15

Example: I want to delete ALL Files in all Subdirectories containing this String:

3015494

These Files are copied more than one time down of the Root-Folder "D:\TEMP\MYTEST" and also copied into the Subdirectories.

I try to write a little function for this. But i can delete Files into a given Folder, but not down into Subfolders ...

Last Code from me:

// call my function to delete files ...
string mypath = @"D:\TEMP\MYTEST\";
string myfilecontains = @"xx";

DeleteFile(mypath, true, myfilecontains);


// some code i found here and should delete just Files,
// but only works in Root-Dir.
// Also will not respect my need for Filename contains Text

public static bool DeleteFile(string folderPath, bool recursive, string FilenameContains)
{
    //Safety check for directory existence.
    if (!Directory.Exists(folderPath))
        return false;

    foreach (string file in Directory.GetFiles(folderPath))
    {
        File.Delete(file);
    }

    //Iterate to sub directory only if required.
    if (recursive)
    {
        foreach (string dir in Directory.GetDirectories(folderPath))
        {
            //DeleteFile(dir, recursive);
            MessageBox.Show(dir);
        }
    }
    //Delete the parent directory before leaving
    //Directory.Delete(folderPath);
    return true;
}

What i have to change in this Code for my needs?

Or is there a complete different code something more helpfull?

I hope you have some good ideas for me to catch the trick.

CodePudding user response:

DirectoryInfo dir = new DirectoryInfo(mypath);
// get all the files in the directory. 
// SearchOptions.AllDirectories gets all the files in subdirectories as well
FileInfo[] files = dir.GetFiles("*.*", SearchOption.AllDirectories);
foreach (FileInfo file in files)
{
     if (file.Name.Contains(myfilecontains))
     {
          File.Delete(file.FullName);
     }
}

This is similar to hossein's answer but in his answer if the directory name contains the value of myfilecontains that file will get deleted as well which I would think you don't want.

CodePudding user response:

//get the list of files in the root directory and all its subdirectories:
string mypath = @"D:\TEMP\MYTEST\";
string myfilecontains = @"xx";
var files = Directory.GetFiles(mypath, "*", SearchOption.AllDirectories).ToList<string>();

//get the list of file for remove
var forDelete = files.Where(x => x.Contains(myfilecontains));

//remove files
forDelete.ForEach(x => {  File.Delete(x); }); 

hope this helps!

  • Related