如果文件名包含特定单词,则从目录中删除文件

发布于 2024-08-08 09:30:58 字数 113 浏览 2 评论 0原文

我需要检查一个目录,看看是否有文件名包含特定关键字的文件,如果有,则将其删除。这可能吗?

例如,删除“C:\Folder”中文件名包含关键字“Apple”的所有现有文件。

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".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小忆控 2024-08-15 09:30:58

要扩展 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);
}

非常小心!

请注意,我已经注释掉了删除命令。运行它并仔细测试它,然后让它真正删除任何内容!

如果您希望递归删除根文件夹的所有子文件夹中的文件,请添加 ,System.IO.SearchOption.AllDirectories);到 GetFiles 调用。

如果您这样做,如果 rootFolderPath 的长度小于大约 4 个字符,则拒绝运行也是一个非常的好主意(一个简单的保护措施,防止删除 C:\ 中的所有内容 - 我已经去过那里并且这样做了,这并不好玩!)

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!!!)

天涯离梦残月幽梦 2024-08-15 09:30:58

您可以使用 System.IO.Directory.GetFiles() 获取 string[] 格式的文件列表。

<罢工>
然后,您可以使用 System.IO.File.ReadAllText() 读取完整文件,或者如果文件很大,请使用 System.IO.File.OpenText() 打开 TextReader代码>.

如果您正在寻找文字关键字,String.Contains() 就是您所需要的。

可以使用 System.IO.File.Delete() 来删除文件。确保文件再次关闭。

编辑,GetFiles() 的 2 个示例:

string[] fileNames = System.IO.Directory.GetFiles(@"C:\");
string[] fileNames = System.IO.Directory.GetFiles(@"C:\", @"*.sys");

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");
秋风の叶未落 2024-08-15 09:30:58
new List<string>(Directory.GetFiles(@"C:\Folder")).ForEach(file => {
    if (file.IndexOf("apple", StringComparison.OrdinalIgnoreCase) >= 0)
        File.Delete(file);
});

或者

new List<string>(Directory.GetFiles(@"C:\Folder")).ForEach(file => {
    Regex re = new Regex("apple", RegexOptions.IgnoreCase);
    if (re.IsMatch(file))
        File.Delete(file);
});
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);
});
究竟谁懂我的在乎 2024-08-15 09:30:58

或多或少,这个:

string DeleteThis = "apple";
string[] Files = Directory.GetFiles(@"C:\Folder");

foreach (string file in Files)
{
    if (file.ToUpper().Contains(DeleteThis.ToUpper()))
    {
        File.Delete(file);
    }
}

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);
    }
}
笑着哭最痛 2024-08-15 09:30:58
new List<string>(Directory.GetFiles(@"C:\Folder")).ForEach(file => { if (file.ToUpper().Contains("apple".ToUpper())) File.Delete(file); });
new List<string>(Directory.GetFiles(@"C:\Folder")).ForEach(file => { if (file.ToUpper().Contains("apple".ToUpper())) File.Delete(file); });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文