Created
November 30, 2020 19:41
-
-
Save koturn/d4350fc5ad32e2fd6dbee63bf764ddbb to your computer and use it in GitHub Desktop.
VRChatの写真をタイムスタンプで整理するやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
namespace MoveFileByTimestamp | |
{ | |
public class Program | |
{ | |
private static readonly TimeSpan DefaultOffsetTs; | |
private static string[] DefaultExts; | |
static Program() | |
{ | |
DefaultOffsetTs = new TimeSpan(6, 0, 0); | |
DefaultExts = new[] { ".png", ".jpg", ".jpeg" }; | |
} | |
private static int Main(string[] args) | |
{ | |
var ot = DefaultOffsetTs; | |
var exts = DefaultExts; | |
var targetDir = args.Length > 0 ? args[0] : "."; | |
if (!Directory.Exists(targetDir)) | |
{ | |
Console.Error.WriteLine($"Target directory doesn't exists: {targetDir}"); | |
return 1; | |
} | |
Directory.EnumerateFiles(".") | |
.AsParallel() | |
.Where(fileName => exts.Any(ext => fileName.EndsWith(ext, StringComparison.OrdinalIgnoreCase))) | |
.Select(fileName => | |
{ | |
var lwt = File.GetLastWriteTime(fileName); | |
return (FileName: fileName, LastWriteTime: lwt, OffsetLastWriteTime: (lwt - ot).Date); | |
}) | |
.GroupBy(tpl => tpl.OffsetLastWriteTime) | |
.ForAll(group => | |
{ | |
var ymd = group.Key.ToString("yyyyMMdd"); | |
Directory.CreateDirectory(ymd); | |
var dstDirPath = Path.Combine( | |
Path.GetDirectoryName(group.First().FileName), | |
ymd); | |
var nFiles = 0; | |
foreach (var fileName in group.Select(tpl => tpl.FileName)) | |
{ | |
var dstFilePath = Path.Combine( | |
dstDirPath, | |
Path.GetFileName(fileName)); | |
if (!File.Exists(dstFilePath)) | |
{ | |
File.Move(fileName, dstFilePath); | |
} | |
nFiles++; | |
} | |
Console.WriteLine($"Moved {nFiles} files into {dstDirPath}"); | |
}); | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment