【问题标题】:Moving oldest folder based on name (YYYYMMDD)根据名称移动最旧的文件夹 (YYYYMMDD)
【发布时间】:2014-06-10 19:48:09
【问题描述】:

我有一个包含 30-40 个文件夹的目录,其中包含 CRM 系统的各种备份文件。

我开发了一个脚本,可以从远程服务器下载文件,并将它们放在带有YYYYMMDD 的文件夹中,但是由于空间限制,我现在需要从目录中移动最旧的文件夹。由于公司的 IT 团队不断在服务器之间移动文件夹,我无法使用文件夹创建日期!

什么是最简单的选择?我查看了:deleting the oldest folder by identifying from the folder name 并尝试订购物品然后执行移动。

我的另一个选择是获取根目录中的所有文件夹名称,解析为类型时间和日期的列表,选择最低(最旧)的选项,然后执行文件移动?

【问题讨论】:

  • “我看过……并尝试过” - 但是?什么对你不起作用?如果您不清楚您的问题与该问题有何不同,则可能会被视为重复。
  • 我也不明白你的问题。如果文件夹名称采用YYYYMMDD 格式,则简单的“字母顺序”应该可以完成这项工作...

标签: c#


【解决方案1】:

这样的事情怎么样:

    bool MoveOldestFolder(string initialFolderName, string destinationFolder)
    {
        // gets all top folders in your chosen location
        var directories = System.IO.Directory.EnumerateDirectories(initialFolderName,"*", System.IO.SearchOption.TopDirectoryOnly);

        // stores the oldest folder and it's date at the end of algorithm
        DateTime outDate;
        DateTime oldestDate = DateTime.MaxValue;
        string resultFolder = string.Empty;

        // just a temp variable
        string tmp;

        // using LINQ
        directories.ToList().ForEach(p =>
        {
            tmp = new System.IO.FileInfo(p).Name; // get the name of the current folder
            if (DateTime.TryParseExact(tmp,
                                       "yyyyMMdd",  // this is case sensitive!
                                       System.Globalization.CultureInfo.InvariantCulture,
                                       System.Globalization.DateTimeStyles.None, 
                                       out outDate)) // try using folder name as date that "should" be in yyyyMMdd format - if the conversion is successful and date is older than current outDate, then store folder name and date, else nothing
            {
                if (outDate.Date < oldestDate.Date)
                {
                    oldestDate = outDate;
                    resultFolder = p;
                }
            }
        });


        // if we actually found a folder that is formatted in yyyyMMdd format
        if (!oldestDate.Equals(DateTime.MaxValue))
        {
            try
            {
                System.IO.Directory.Move(resultFolder, destinationFolder);

                return true;
            }
            catch(Exception ex)
            {
                // handle the excaption
                return false;
            }
        }
        else
        {
            // we didnt find anything
            return false;
        }
    }

private void button1_Click(object sender, EventArgs e)
{
    var initialFolderName = @"C:\initial";
    var destinationFolder = @"c:\dest";

    if (MoveOldestFolder(initialFolderName, destinationFolder))
    {
        // move was successful                
    }
    else
    {
        // something went wrong
    }
}

其他选择是简单地按照chrfin 所说的去做,但我不会假设文件夹结构中的所有内容都是“花花公子”。文件夹名称总是有可能不是 YYYYMMDD 格式,这可能会导致我想象的一些问题。 无论如何,代码可能看起来像这样:

    var directories = System.IO.Directory.EnumerateDirectories(initialFolderName,"*", System.IO.SearchOption.TopDirectoryOnly);
    directories.ToList<string>().Sort();
    var lastDir = directories.First();

【讨论】:

  • 太好了,正是我想要的:)
猜你喜欢
  • 1970-01-01
  • 2017-06-07
  • 2021-12-26
  • 2021-11-06
  • 2013-01-15
  • 1970-01-01
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多