毎日作成するファイルを自動リネームする
using System;
using System.IO;
namespace console_sample
{
internal class Program
{
private const string TemplatePath = "../../resources/daily_yyyymmdd.txt";
private const string DestPath = "../../dest/";
public static void Main(string[] args)
{
Console.WriteLine("-- start --");
var pwd = Directory.GetCurrentDirectory();
Console.WriteLine("pwd: " + pwd);
var today = DateTime.Now;
// コピー元ファイルを決める
var srcPath = "../../dest/";
// todo: 1週間内の直近のファイルを読み込む
var srcDay = today.AddDays(-1);
do
{
var p = "daily_" + srcDay.ToString("yyyy-MM-dd") + ".txt";
Console.WriteLine($"p: {p}");
if (File.Exists(DestPath + p))
{
Console.WriteLine($"exists!");
srcPath += p;
break;
}
srcDay = srcDay.AddDays(-1);
} while (!srcDay.Equals(today.AddDays(-7)));
if (srcPath.Equals("../../dest/"))
{
srcPath = TemplatePath;
}
Console.WriteLine("src: " + srcPath);
var d = today.ToString("yyyy-MM-dd");
File.Copy(srcPath, DestPath + $"daily_{d}.txt", true);
Console.WriteLine("-- end --");
}
}
}