【问题标题】:How to create folders from a list of files by last modified date month wise c#如何按最后修改日期按月从文件列表中创建文件夹c#
【发布时间】:2016-01-17 15:09:27
【问题描述】:

我想从文件列表中按月创建文件夹。

我试过下面的代码。

var files = directory.GetFiles()
                  .Where(file => file.LastWriteTime.Month == date.Month -1);

             //create folder for the files (With MonthName)
             var year = files.Select(j => j.LastWriteTime.Year).FirstOrDefault();
             var month = files.Select(j => j.LastWriteTime.Month).FirstOrDefault();
             var newFolderPath = year.ToString() + month.ToString();

             var destinationDirec = System.IO.Directory.CreateDirectory(directory + newFolderPath);

             foreach (var f in files)
             {                 

               // var destFile = System.IO.Path.Combine(directory, destinationDirec);
                 var path = Path.Combine(destinationDirec.FullName, f.Name);

                 if (!File.Exists(path))
                 {
                     System.IO.File.Move(f.FullName, path);
                 }                  

             }               

上面的代码给出了上个月的文件列表。但我想为早于本月的文件创建文件夹。 谁能给我一个解决方案?

【问题讨论】:

  • 您想按月份对所有文件进行排序,然后将它们放置到具有适当名称的单独文件夹中吗?
  • 查找最大和最小日期并在它们之间的每个月/年迭代以创建文件夹。然后,在编写时,使用现有的var year ...var month 来获取实际目的地。详细信息取决于您预期的日期范围。
  • @kote - 是的,你是对的。

标签: c# asp.net .net system.io.file


【解决方案1】:

你可以试试这个代码。也许会有一些变化。

//Group files by month. Later you can skip some groups if needed
var fileGroups = directory.GetFiles()
    .GroupBy(file => file.LastWriteTime.Month);

foreach (var fileList in fileGroups)
{
    var year = fileList.First().LastWriteTime.Year;
    var month = fileList.First().LastWriteTime.Month;
    var newFolderPath = year.ToString() + month.ToString();
    var destinationDirec = System.IO.Directory.CreateDirectory(directory + newFolderPath);

    //move files
    foreach (var file in fileList)
    {
        var path = Path.Combine(destinationDirec.FullName, file.Name);
        if (!File.Exists(path))
        {
            System.IO.File.Move(file.FullName, path);
        }
    }
}

如果您有很多不同年份的文件,也许值得修改GroupBy 条件。
例如你可以使用这个条件:
GroupBy(file => (397 * file.LastWriteTime.Year) ^ file.LastWriteTime.Month)

【讨论】:

    【解决方案2】:

    这应该会有所帮助,根据需要构建 fullNewDir 值。

    String fullSourceDir = "G:\\Tmp\\Test";     
    foreach (var fullFileName in Directory.EnumerateFiles(fullSourceDir)){
        DateTime lastWriteTime = File.GetLastWriteTime(Path.Combine(fullSourceDir, fullFileName));
        String fullNewDir = Path.Combine(fullSourceDir, lastWriteTime.ToString("yyyy-MM-dd_HH-mm"));
        if (!Directory.Exists(fullNewDir)){
            Directory.CreateDirectory(fullNewDir);
        }
        String fileName = Path.GetFileName(fullFileName);
        System.IO.File.Move(fullFileName, Path.Combine(fullNewDir, fileName));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      相关资源
      最近更新 更多