-
Notifications
You must be signed in to change notification settings - Fork 0
/
Directory.cs
104 lines (87 loc) · 2.44 KB
/
Directory.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BatchRegex
{
public class Dir
{
public Dir()
{
FileList = new List<File>();
}
public Dir(string newPath)
{
FileList = new List<File>();
Path = newPath;
Update();
}
private string _Path;
public string Path
{
get
{
return this._Path;
}
set
{
this.Clear();
this._Path = value;
this.Update();
}
}
public List<File> FileList
{
get;
set;
}
private void AddFile(string fileName){
File newFile = new File(fileName, this);
this.FileList.Add(newFile);
}
public void Remove(File fileToRemove)
{
FileList.Remove(fileToRemove);
}
// Clear every file from the list
public void Clear()
{
this.FileList.Clear();
}
// Add all the files from the directory specified in Path to the list
public void Update() // Throws exceptions
{
this.Clear();
string[] fileNamesList = System.IO.Directory.GetFiles(this.Path);
foreach (string fileName in fileNamesList)
{
this.AddFile(System.IO.Path.GetFileName(fileName));
}
}
// Apply the regex on every file
public void ApplyRegex(SimpleRegex regex)
{
foreach (File file in this.FileList)
{
file.ApplyRegex(regex);
}
}
// Rename the files
public Stack<RenameAction> RenameFiles()
{
Console.WriteLine("[" + Path + "]");
Stack<RenameAction> history = new Stack<RenameAction>();
foreach (File file in this.FileList)
{
Console.WriteLine(" " + file.FileName + " ==> " + file.RegexName);
RenameAction result = file.Rename();
if (result != null)
{
history.Push(result);
}
}
return history;
}
}
}