使用正则表达式重命名文件

时间:2012-06-19 12:04:39

标签: c# .net regex windows-7-x64

我有一组文件格式相同somethingname___someotherstuff.txt

我想乘坐___someotherstuff位。

___ = 3下划线)

这是字母和数字的组合

我该怎么做?

编辑:操作系统= Windows 7

再次编辑:'someotherstuff'位在每个文件中都不相同。不同数字和字符的组合

再次编辑:有人回答了它然后撤回了它。为什么?你有正确的答案。 _ \ w + 谢谢。如果你愿意,可以把它放回去,我会把你的标记作为答案

2 个答案:

答案 0 :(得分:0)

你可以大致这样做:

string[] files = Directory.GetFiles(path);
foreach (string f in files)
{
    if (file.IndexOf("___") != -1)
    {
        File.Move(file, Regex.Replace(file, "___.*\.txt$", ""));
    }
}

答案 1 :(得分:0)

以下正则表达式可能满足要求......

string newFileName = Regex.Replace(fileName, @"___\w*(?=\.)", "")