-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
109 lines (94 loc) · 3.88 KB
/
Copy pathProgram.cs
File metadata and controls
109 lines (94 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
namespace PicSort
{
/// Goal to have all files distributed in the directories per month
/// 2011-10
/// `----- 2011-10-03_15-12-13.jpg
class Program
{
static void Main(string[] args)
{
if (args == null || args.Length != 3)
{
Console.WriteLine("{srcDir} {dstDir} {fileFilter}");
return;
}
var srcDir = args[0];
var dstDir = args[1];
var fileFilter = args[2]; // : "*.jpg";
Console.WriteLine($"FROM: [{srcDir}] TO: [{dstDir}] FILTER: [{fileFilter}]");
Process(srcDir, dstDir, fileFilter);
}
// Directory confirms format YYYY-MM
// Therefore already was processed by the tool
private static bool SkipFile(string lastDir)
{
// YYYY-MM
if ((lastDir.Length == 7) && _yyyyMM.IsMatch(lastDir))
{
return true;
}
return false;
}
private static Regex _yyyyMM = new Regex(@"\d{4}-\d{2}",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static void Process(string srcDir, string dstDir, string fileFilter)
{
HashSet<string> newDirs = new HashSet<string>();
try
{
var files = Directory.EnumerateFiles(srcDir, fileFilter,
SearchOption.AllDirectories);
int ind = 0;
bool inputIs0000_01 = Path.GetFileName(srcDir) == "0001-01";
foreach (string fullName in files)
{
++ind;
Console.WriteLine($"Start processing [{ind}] {fullName}");
var fullDirName = Path.GetDirectoryName(fullName);
var lastDir = Path.GetFileName(fullDirName);
if (!inputIs0000_01 && SkipFile(lastDir))
{
continue;
}
var fileExt = Path.GetExtension(fullName).ToLower();
var fileName = Path.GetFileName(fullName);
var fileInfo = ExtractFileMetaInfo(fileName, fullName, fileExt);
if (fileInfo == null)
{
Console.WriteLine($"Could not handle {fullName}");
continue;
}
var fileDstDir = Path.Combine(dstDir, fileInfo.DirName);
if ( !newDirs.Contains(fileInfo.DirName) )
{
if (!Directory.Exists(fileDstDir))
{
Directory.CreateDirectory(fileDstDir);
}
newDirs.Add(fileInfo.DirName);
}
var newFullName = Path.Combine(fileDstDir, fileName);
Console.WriteLine($"Moving {fullName} to {newFullName}");
if (!File.Exists(newFullName))
{
File.Move(fullName, newFullName);
}
// string fileName = currentFile.Substring(sourceDirectory.Length + 1);
// Directory.Move(currentFile, Path.Combine(archiveDirectory, fileName));
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
private static FileMetaInfo ExtractFileMetaInfo(string fileName, string fullName, string fileExt)
{
return FileMetaInfo.MetaInfoFactory(fullName, fileName, fileExt);
}
}
}