2

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

比如说,如果文件名 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)(我可以再次删除-)。要求用户选择这样的文件夹

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;
            }
        }

此外,它的重命名是这样开始的

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);
}

现在在重命名文件中,我需要函数,即删除文件中任何数字的正则表达式。它被实现为

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 */ 
}

所以我能做的是,我可以使用类似的东西

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;

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

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

4

5 回答 5

1

您可以尝试 Regex.Replace 删除数字,即:

Regex.Replace(fileName, @"\d", "");
于 2012-04-12T04:21:42.993 回答
1

In the off chance that you are merely looking to simply rename the files and you thought that creating your own program would be the best way - I would recommend PFrank as a standalone tool (especially if you understand regex already)

If you do desire this and if you do take my suggestion (and since it's not the simplest and clearest interface), you would use \d+(\s?-)? for the match expression (in the first column in PFrank), which should match any number of digits, optionally followed by a hyphen and an additional optional whitespace character between the two. You would then have no replacement expression (zero-length string or an empty second column in PFrank). Finally, select the folder containing the files you want renamed and click the scan button; in the dialog that pops up, confirm your results and click the rename button. Sorry if I wasted anyone's time!

于 2012-04-12T14:27:05.597 回答
0

对于替换,您应该查看可以一次替换所有出现的Regex.Replace

否则代码看起来没问题(除了fileName.Replace("match", "")使用常量字符串的奇怪......)

于 2012-04-12T04:21:28.077 回答
0

这个怎么样 ?

private void StartRenaming(string FolderName)
{
    string[] files = Directory.GetFiles(FolderName);
    string[] applicableFiles = (from string s in files
                                where Regex.IsMatch(s, @"(\d+)|(-+)", RegexOptions.None)
                                select s).ToArray<string>();

    foreach (string file in applicableFiles)
        RenameFile(file);
}

private void RenameFile(string file)
{
    string newFileName = Regex.Replace(file, @"(\d+)|(-+)", "");
    File.Move(file, Path.Combine(Path.GetDirectoryName(file), newFileName));
}

StartRenaming方法现在将根据Regex匹配限制要处理的文件数。如果文件包含一个数字,-那么它将被处理,从而优化整个过程。 RenameFile替换-字符串中的数字和并为您提供newFileName

我不太确定它的正确性File.Move(file, Path.Combine(Path.GetDirectoryName(file), newFileName));,但我想你的问题是避免foreach循环,我想我已经提供了一个合适的解决方案。

Please note that I was not able to completely test this, so let me know whether it works for you and if it doesn't I will be happy to help you further.

EDIT : Forgot to mention that file.Replace(@"(\d+)|(-+)", "") will remove digits as well as - from the file string.

EDIT : Corrected file.Replace to Regex.Replace

于 2012-04-12T04:33:36.813 回答
0

I prefer to use brackets to select the before and after and then use the $n method to rebuild the string how you want it to be.

"03 - Rocketman Elton John"  -Replace '^([^-]*) - ([^-]*)', '$1 $2'
于 2012-11-30T23:32:44.523 回答