本文介绍了使用正则表达式重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是重命名特定文件夹中的所有文件,这样,如果文件名中包含任何数字,则将其删除.

What I want to do is rename all file in a particular folder, such that if a filename contains any digit in it, it is removed.

假设,如果文件名是someFileName.someExtension它保持不变,但是如果文件是这样的,03 - Rocketman Elton John应该将其重命名为Rocketman Elton John(我做了删除-的部分),另一个示例,如果文件名是15-Trey Songz - Unfortunate (Prod. by Noah 40 Shebib),则应将其重命名为Trey Songz Unfortunate (Prod. by Noah Shebib)(同样,我可以删除- ).要求用户选择这样的文件夹

say, if a filename issomeFileName.someExtension it remains the same, but if a file is like this,03 - Rocketman Elton John it should be renamed to Rocketman Elton John (I did the part to remove the -), another example, if the filename is 15-Trey Songz - Unfortunate (Prod. by Noah 40 Shebib) it should be renamed to Trey Songz Unfortunate (Prod. by Noah Shebib) (again I can remove -). The user is asked to select the folder like this

private void txtFolder_MouseDown(object sender, MouseEventArgs e)
        {
            FolderBrowserDialog fd = new FolderBrowserDialog();
            fd.RootFolder = Environment.SpecialFolder.Desktop;
            fd.ShowNewFolderButton = true;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                txtFolder.Text = fd.SelectedPath;
            }
        }

而且,重命名是这样开始的

Also, it renaming starts like this

private void btnGo_Click(object sender, EventArgs e)
{
    StartRenaming(txtFolder.Text);
}

private void StartRenaming(string FolderName)
{
   string[] files = Directory.GetFiles(FolderName);
   foreach (string file in files)
        RenameFile(file);
}

现在在重命名文件中,我需要函数,该正则表达式将删除文件中的任何数字.其实现为

Now in rename file, I need the function, the regular expression that will remove any number(s) in file.Its is implemented as

private void RenameFile(string FileName)
{
    string fileName = Path.GetFileNameWithoutExtension(FileName);
   /* here the function goes that will find numbers in filename using regular experssion and replace them */
}

所以我可以做的是,我可以使用

so what I can do is, I can use something like

1  var matches = Regex.Matches(fileName, @"\d+");
2
3  if (matches.Count == 0)
4    return null;
5
6  // start the loop
7  foreach(var match in matches)
8  {
9     fileName = fileName.Replace(match, ""); /* or fileName.Replace(match.ToString(), ""), whatever be the case */
10 }
11  File.Move(FileName, Path.Combine(Path.GetDirectoryName(FileName), fileName));
12  return;

但是我不认为这是正确的方法?有什么更好的选择吗?还是这是最好的(也是唯一的选择)?另外,String.Replace中是否有类似IN的内容?在sql中说我可以在select命令中使用IN并指定一堆where条件,但是String.Replace是否有类似的东西,这样我就不必运行从第7行到第10行的循环了?还有其他更好的选择吗?

But I don't think that's the right way to do it? Is there any better option to do this? or is this the best (and only option) to do this? Also, is there anything like IN in String.Replace? Say in sql I can use IN in a select command and specify a bunch of where conditions, but is there something like this with String.Replace so that I don't have to run the loop I ran from line 7 to 10? Are there any other better options?

ps:关于该正则表达式,我发布了一个问题数字的正则表达式?(显然我还不够清楚),因此我得到了我的正则表达式,如果您认为其他正则表达式会更好,请告诉我,如果您需要任何其他信息,请告诉我...

ps: about that regex, I posted a question Regular Expression for numbers? (apparently I wasn't clear enough) and from that I got my regex, if you think someother regex would do better please tell me, also if you need any other information please let me know...

推荐答案

要进行替换,您应该查看 Regex.Replace ,它可以一次替换所有出现的内容.

For replacing you should look into Regex.Replace which can replace all occurences at once.

否则,代码看起来还可以(奇怪的fileName.Replace("match", "")使用常量字符串...除外)

Otherwise code look ok (with exception of strange fileName.Replace("match", "") which uses constant string...)

这篇关于使用正则表达式重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:16