You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C#和.NET按创建日期将文件移入对应格式文件夹?

问题:按文件修改日期归档所有文件到对应目录

现有代码仅能将源文件夹中今日创建/修改的文件移入C:/root/yyyy/MM/dd格式的目标目录,需要修改为:所有文件(无论新旧)都按自身的修改日期归档到对应年份/月份/日期的子目录中。

现有代码(仅处理今日文件)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace VPorganizer
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Created variables for root directory and rundate.
            string rootdir = @"C:\Users\memyself\Documents\VPOrgTEST";
            DateTime rundate = DateTime.Now;
            string runyear = rundate.ToString("yyyy");
            string runmonth = rundate.ToString("MM");
            string runday = rundate.ToString("dd");
            
            string subdir = String.Join(@"\", rootdir, runyear, runmonth, runday) + @"\";
            Directory.CreateDirectory(subdir);


            //This returns a file list from the root directory.
            DirectoryInfo di = new DirectoryInfo(rootdir);
            FileInfo[] fi = di.GetFiles();
            
            //This looks for files with today's date and compares it to the rundate. If there's a match, it moves the file to the folder corresponding to the last write time.
            foreach (FileInfo file in fi) 
            {
                string destfile = subdir + file.Name;
                if (file.LastWriteTime.ToString("yyyyMMdd") == rundate.ToString("yyyyMMdd")) 
                {
                    File.Move(file.FullName, destfile);   
                }

            }
        }
    }
}

尝试过程中的问题

  • 第一次尝试代码逻辑不完整,仅移动了单个文件
  • 第二次尝试错误地将所有旧文件移入今日目录,原因是固定使用rundate生成目标路径,未根据文件自身日期动态调整

解决方案:动态根据文件日期生成目标目录

核心修改点:

  1. 移除固定的rundate目录生成逻辑,改为针对每个文件,用其自身的LastWriteTime生成对应目录
  2. 使用Path.Combine拼接路径,避免手动拼接斜杠的错误
  3. 无需判断文件日期,直接处理所有根目录下的文件

修改后的完整代码:

using System;
using System.IO;

namespace VPorganizer
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string rootdir = @"C:\Users\memyself\Documents\VPOrgTEST";
            DirectoryInfo di = new DirectoryInfo(rootdir);
            FileInfo[] fi = di.GetFiles();

            foreach (FileInfo file in fi)
            {
                // 提取文件修改日期的年、月、日字符串
                string fileYear = file.LastWriteTime.ToString("yyyy");
                string fileMonth = file.LastWriteTime.ToString("MM");
                string fileDay = file.LastWriteTime.ToString("dd");

                // 拼接目标目录路径
                string targetDir = Path.Combine(rootdir, fileYear, fileMonth, fileDay);
                // 自动创建不存在的目录(含父级目录)
                Directory.CreateDirectory(targetDir);

                // 拼接目标文件完整路径
                string targetFilePath = Path.Combine(targetDir, file.Name);
                // 移动文件到目标位置
                File.Move(file.FullName, targetFilePath);
            }
        }
    }
}

关键说明

  • Path.Combine会自动适配系统路径分隔符,比手动拼接更可靠
  • Directory.CreateDirectory会一次性创建所有缺失的父目录(比如2023/06/19,即使2023和06目录不存在也会自动创建)
  • 如果需要用文件创建日期替代修改日期,只需将file.LastWriteTime替换为file.CreationTime即可

内容的提问来源于stack exchange,提问作者user22671676

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 05:17:33