Delete files from directory if filename contains a certain word

前端 未结 5 1517
不知归路
不知归路 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:42

    new List(Directory.GetFiles(@"C:\Folder")).ForEach(file => {
        if (file.IndexOf("apple", StringComparison.OrdinalIgnoreCase) >= 0)
            File.Delete(file);
    });
    

    or

    new List(Directory.GetFiles(@"C:\Folder")).ForEach(file => {
        Regex re = new Regex("apple", RegexOptions.IgnoreCase);
        if (re.IsMatch(file))
            File.Delete(file);
    });
    

提交回复
热议问题