如何递归地使用匹配字符串重命名文件夹名称和文件名

问题描述 投票:0回答:3

我的应用程序在以下目录结构中生成文件

FolderMatchALeve1
-FileMatchALevel2_A.cs
-FileMatchALevel2_B.cs
-FolderMatchALevel2
--FileMatchALevel3_A.txt
--FileMatchALevel3_B.txt

我正在寻找一种重命名目录结构的方法,并进行以下更改 – 将 “MatchA” 更改为 “AMatch”

所以执行程序后的结果应该是这样的:

FolderAMatchLeve1
-FileAMatchLevel2_A.cs
-FileAMatchLevel2_B.cs
-FolderAMatchLevel2
--FileAMatchLevel3_A.txt
--FileAMatchLevel3_B.txt

到目前为止,我在寻找解决方案的过程中一直没有成功。请帮我找到解决方案。

我需要 C# Winforms 中的这个解决方案,因为我们公司维护遗留产品。

编辑:

其他信息

  1. 每次有人运行我们的程序时我都需要进行此更改。

  2. 我需要对 3350 个文件执行此操作

问题摘要:

简而言之,在递归(或迭代)地遍历每个目录时,我希望它重命名名称与匹配字符串匹配的文件,然后在出来后重命名该目录,如果它也有一个与字符串匹配的名称(对于所有部分或完成匹配)。

c# string winforms directory
3个回答
1
投票

又快又脏(但有效)

public static class DirectoryRenamer
{
    public static void RenameDirectoryTree( string path, Func<string, string> renamingRule )
    {
        var di = new DirectoryInfo( path );
        RenameDirectoryTree( di, renamingRule );
    }

    public static void RenameDirectoryTree( DirectoryInfo directory, Func<string, string> renamingRule )
    {
        InternalRenameDirectoryTree( directory, renamingRule );

        var currentName = directory.Name;
        var newName = renamingRule( currentName );
        if ( currentName != newName )
        {
            var newDirname = Path.Combine( directory.Parent.FullName, newName );
            directory.MoveTo( newDirname );
        }
    }

    static void InternalRenameDirectoryTree( DirectoryInfo di, Func<string, string> renamingRule )
    {
        foreach ( var item in di.GetFileSystemInfos() )
        {
            var subdir = item as DirectoryInfo;
            if ( subdir != null )
            {
                InternalRenameDirectoryTree( subdir, renamingRule );

                var currentName = subdir.Name;
                var newName = renamingRule( currentName );
                if ( currentName != newName )
                {
                    var newDirname = Path.Combine( subdir.Parent.FullName, newName );
                    subdir.MoveTo( newDirname );
                }
            }

            var file = item as FileInfo;
            if ( file != null )
            {
                var currentName = Path.GetFileNameWithoutExtension( file.Name );
                var newName = renamingRule( currentName );
                if ( currentName != newName )
                {
                    var newFilename = Path.Combine( file.DirectoryName, newName + file.Extension );
                    file.MoveTo( newFilename );
                }
            }
        }
    }
}

使用示例

class Program
{
    static void Main( string[] args )
    {
        DirectoryRenamer.RenameDirectoryTree( 
            @"C:\Test\FolderMatchALevel", 
            name => name.Replace( "MatchA", "AMatch" ) );
    }
}

1
投票

使用通配符,我曾经能够使用基本的移动命令更改多个文件的文件结尾。但这些改变可能有点超出了。

但总的来说,这只是使用 Directory 类或其他类之一对文件夹进行微不足道的递归。 伪代码如下:

  1. 列出当前目录下的所有目录。
  2. 对所有子目录再次递归调用此函数
  3. 迭代此目录中的所有文件
  4. 重命名此目录。

请注意,重命名的“正确”方法是移动命令。事实上,在同一磁盘上移动和重命名之间没有技术上的区别。

您可能还想输入 Nr。 4 在布尔开关上。将参数之一命名为“DirectoryRename”。让它默认为true。在第一次调用时将其传递为 false,并且不要将其用于递归调用。


-1
投票

这很棒,但我想做的比这简单得多。我只想删除文件名和文件夹名称前面的某些字符?我该怎么做?

© www.soinside.com 2019 - 2024. All rights reserved.