C# 如果文件名包含某个单词,则从目录中删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1620366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete files from directory if filename contains a certain word
提问by user
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".
例如,删除“ C:\Folder
”中所有文件名中包含关键字“Apple”的现有文件。
采纳答案by Jason Williams
To expand on Henk's answer, you need:
要扩展 Henk 的答案,您需要:
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 carefullybefore 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.
如果您希望递归删除根文件夹的所有子文件夹中的文件,请添加 ,System.IO.SearchOption.AllDirectories); 到 GetFiles 调用。
If you do this it is also a verygood 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!!!)
如果你做到这一点也是一个很不错的主意,拒绝运行,如果rootFolderPath小于约4个字符长(一个简单的保护,以免受C删除一切:\ -我一直在那里,做到这一点,它是不好玩! !!)
回答by Henk Holterman
You can use System.IO.Directory.GetFiles()
to a list of the files, in string[] format.
您可以使用System.IO.Directory.GetFiles()
string[] 格式的文件列表。
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()
.
然后你可以使用System.IO.File.ReadAllText()
读取完整的文件,或者如果它们非常大,打开一个带有System.IO.File.OpenText()
.
If you are looking for a literal keyword, String.Contains()
is all you need.
如果您正在寻找文字关键字,这String.Contains()
就是您所需要的。
Deleting a file can be done with System.IO.File.Delete()
. Make sure the file is closed again.
删除文件可以使用System.IO.File.Delete()
. 确保再次关闭文件。
Edit, 2 examples of GetFiles()
:
编辑,2 个例子GetFiles()
:
string[] fileNames = System.IO.Directory.GetFiles(@"C:\");
string[] fileNames = System.IO.Directory.GetFiles(@"C:\", @"*.sys");
回答by Kyle Rozendo
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);
}
}
回答by Antony Koch
new List<string>(Directory.GetFiles(@"C:\Folder")).ForEach(file => { if (file.ToUpper().Contains("apple".ToUpper())) File.Delete(file); });
回答by Keith
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);
});