C# 如果文件在存档时已经存在,那么这是一种优雅的重命名方法

C# 如果文件在存档时已经存在,那么这是一种优雅的重命名方法,c#,C#,我想写一个存档函数,它将所有文件都放在一个文件夹中,并使用当前日期将它们移动到存档子文件夹中。该进程可能在一天内运行几次,因此需要处理重复项 存档规则如下: 如果文件名已经存在,我想添加一个下划线uu和 从1开始的数字。 如果文件名已被修改,我希望 增加数字。 我可以使用大量的File.Exist和LastIndexOf调用来实现这一点,但是有没有更优雅的方法呢?也许和林克在一起 编辑: 这是我已经有的代码。这有点粗糙,但它给了我一个想法,我想做什么 /// <summary> //

我想写一个存档函数,它将所有文件都放在一个文件夹中,并使用当前日期将它们移动到存档子文件夹中。该进程可能在一天内运行几次,因此需要处理重复项

存档规则如下:

如果文件名已经存在,我想添加一个下划线uu和 从1开始的数字。 如果文件名已被修改,我希望 增加数字。 我可以使用大量的File.Exist和LastIndexOf调用来实现这一点,但是有没有更优雅的方法呢?也许和林克在一起

编辑:

这是我已经有的代码。这有点粗糙,但它给了我一个想法,我想做什么

/// <summary>
/// Move the local file into the archive location.
/// If the file already exists then add a counter to the file name or increment the existing counter
/// </summary>
/// <param name="LocalFilePath">The full path of the file to be archived</param>
/// <param name="ArchiveFilePath">The proposed full path of the file once it's been archived</param>
private void ArchiveFile(string LocalFilePath, string ArchiveFilePath)
{
    // Ensure the file name doesn't already exists in the location we want to move it to
    if (File.Exists(ArchiveFilePath) == true)
    {
        // Does the archive file have a number at the end?
        string[] archiveSplit = Path.GetFileNameWithoutExtension(ArchiveFilePath).Split('_');
        if( archiveSplit.Length == 1)
        {
            // No number detected so append the number 1 to the filename
            string newArcFileName = string.Format("{0}_1.{1}",
                Path.GetFileNameWithoutExtension(ArchiveFilePath), Path.GetExtension(ArchiveFilePath));

            // Create the new full path
            string newArcPath = Path.Combine(Path.GetDirectoryName(ArchiveFilePath), newArcFileName);

            // recursively call the archive folder to ensure the new file name doesn't exist before moving
            ArchiveFile( LocalFilePath, newArcPath);
        }
        else
        {
            // Get the number of the last element of the split
            int lastNum = Convert.ToInt32( archiveSplit.Last().Substring(1) ) +1;

            // Rebuild the filename using the incremented number
            string newArcFileName = archiveSplit[0];
            for (int i = 1; i < archiveSplit.Length; i++)
            {
                newArcFileName += archiveSplit[i];
            }
            // finally add the number and extension
            newArcFileName = string.Format("{0}_{1}.{2}", newArcFileName, lastNum, Path.GetExtension(ArchiveFilePath));

            // Create the new full path
            string newArcPath = Path.Combine(Path.GetDirectoryName(ArchiveFilePath), newArcFileName);

            // recursively call the archive folder to ensure the new file name doesn't exist before moving
            ArchiveFile(LocalFilePath, newArcPath);
        }
    }
    else
    {
        // There is no file with a matching name
        File.Move(LocalFilePath, ArchiveFilePath);
    }
}
即使使用LINQ,仍然需要调用File.Exists,这不会改变

我建议保持简单-使用File.Exists和LastIndexOf循环是一个合适的解决方案,除非性能是必需的。

File.Exists仍然需要调用,即使您使用LINQ,也不会改变

我建议保持简单-使用File.Exists和LastIndexOf循环是一个合适的解决方案,除非性能是必需的。

目录类有一个方法来接收其中所有文件的列表。该方法允许您指定筛选器字符串,如下所示:

Directory.GetFiles(directoryPath, filterString);
如果您已经知道文件名前缀,则可以使用该筛选字符串获取该模式中的所有文件:

filterString = string.Format("{0}_*.{1}", defaultFileNameWithoutExtension, defaultExtension);
然后,您只需选择具有最高后缀的一个,提取后缀数字,增加它,并构建新的未使用文件名

免责声明:这是用心写的,如果出现错误,请随意编辑:

目录类有一个方法来接收目录中所有文件的列表。该方法允许您指定筛选器字符串,如下所示:

Directory.GetFiles(directoryPath, filterString);
如果您已经知道文件名前缀,则可以使用该筛选字符串获取该模式中的所有文件:

filterString = string.Format("{0}_*.{1}", defaultFileNameWithoutExtension, defaultExtension);
然后,您只需选择具有最高后缀的一个,提取后缀数字,增加它,并构建新的未使用文件名


免责声明:这是用心写的,如果出现错误,请随意编辑:

也许,您应该使用路径API并使用EndsWith而不是LastIndexOf:

你也可以有一个文件来存储文件树。关注rsync

即使相同的文件没有更改,您是否真的要复制多个相同的文件?您正在查找更新的修改日期时间吗


:Path

也许,您应该使用Path API并使用EndsWith而不是LastIndexOf:

你也可以有一个文件来存储文件树。关注rsync

即使相同的文件没有更改,您是否真的要复制多个相同的文件?您正在查找更新的修改日期时间吗


:Path

我来看看EndsWith。我已经在使用文件和路径类了。我会看看EndsWith。我已经在使用文件和路径类了。