Delete files from directory if filename contains a certain word

前端 未结 5 1518
不知归路
不知归路 2020-12-30 00:15

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 exampl

5条回答
  •  孤城傲影
    2020-12-30 00:49

    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);
        }
    }
    

提交回复
热议问题