-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
362 lines (308 loc) · 10.8 KB
/
Form1.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Microsoft.WindowsAPICodePack.Dialogs;
namespace mp3_rename
{
public partial class formRenameFiles : Form
{
public formRenameFiles()
{
InitializeComponent();
this.Icon = Properties.Resources.shark_ninja_icon;
this.playing = false;
CreateMediaPlayer();
PickFolder();
}
private void PickFolder()
{
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
//dialog.InitialDirectory = "C:\\Users";
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
//MessageBox.Show("You selected: " + dialog.FileName);
//string[] args = Environment.GetCommandLineArgs();
//if (args[args.Length - 1] == "-debug") {
// OpenFiles_Local(); // for local testing
// return;
//}
OpenFiles(dialog.FileName);
}
else
{
MessageBox.Show("Did not pick a folder... Restart the program to pick a location.", "error");
}
}
private void OpenFiles_Local()
{
this.folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
Directory.SetCurrentDirectory(this.folderPath);
ReloadFiles();
}
private void CreateMediaPlayer()
{
string progId = "WMPlayer.OCX.7";
Type type = Type.GetTypeFromProgID(progId);
this.player = Activator.CreateInstance(type);
}
private string folderPath;
private string syncFolder;
private bool playing;
private dynamic player;
private void RenameFiles()
{
// special case: "25 = 34式太极"
//
// "34太极",
// "34式太极",
// "25 =34太极",
// "25 = 34太极",
// "25 = 34式太极",
// "25 = 34路太极",
// "25 = 34套太极",
// "10 - 太极",
// "abc",
//
string prefix = @"^[0-9]* - ";
Regex reMain = new Regex(prefix + @"(.+)");
int n = 0,
total = listViewFiles.Items.Count;
bool changed = false;
int padToZeros = (int) Math.Ceiling(Math.Log10(total + 1));
foreach (ListViewItem item in listViewFiles.Items)
{
var fileName = item.Text;
var original = fileName;
n ++;
Match m = reMain.Match(fileName);
if (m.Success)
{
// just replace regex
fileName = m.Groups[1].Value;
}
// otherwise use full filename and add regex
fileName = n.ToString("D" + padToZeros) + " - " + fileName;
if (fileName != original)
{
File.Move(original + ".mp3", fileName + ".mp3");
changed = true;
}
}
if (!changed)
{
return;
}
StopPlay();
if (!RefreshOrdersOnDisk())
{
//
// Refresh one more time, which seems solving majority of problems.
//
if (!RefreshOrdersOnDisk())
{
//MessageBox.Show("文件没有正确排序", "出错");
MessageBox.Show("The files are not sorted correctly", "error");
}
}
SyncFiles();
ReloadFiles();
}
private bool RefreshOrdersOnDisk()
{
//
// "Many MP3 players that are based on USB flash drives don't allow you to sort the MP3 files
// in the order you want to listen to them. Instead they play the MP3 files in the order they
// find them; usually the order you copied them to the flash drive. How do we re-order the files?"
//
// https://blogs.msdn.microsoft.com/oldnewthing/20140304-00/?p=1603
// What order does the DIR command arrange files if no sort order is specified?
//
// "But the easy way out is simply to remove all the files from a directory then move file files
// into the directory in the order you want them enumerated. That way, the first available slot
// is the one at the end of the directory, so the file entry gets appended."
//
string tempDir = GetTempDirectory();
MoveFiles(".", tempDir);
MoveFiles(tempDir, ".");
Directory.Delete(tempDir);
var files = Directory.GetFiles(this.folderPath, "*.mp3");
return IsSorted(files);
}
/// <summary>
/// Determines if string array is sorted from A -> Z
/// </summary>
public static bool IsSorted(string[] arr)
{
for (int i = 1; i < arr.Length; i++)
{
if (arr[i - 1].CompareTo(arr[i]) > 0) // If previous is bigger, return false
{
return false;
}
}
return true;
}
private string GetTempDirectory()
{
var random = new Random();
while (true)
{
string path = "mp3-" + random.Next(9999).ToString("D4") + ".dir";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
return path;
}
}
}
private void MoveFiles(string sourceDir, string destDir)
{
sourceDir = Path.Combine(this.folderPath, sourceDir);
destDir = Path.Combine(this.folderPath, destDir);
var files = Directory.GetFiles(sourceDir, "*.mp3");
Array.Sort(files);
foreach (var file in files)
{
string fileName = Path.GetFileName(file);
File.Move(file, Path.Combine(destDir, fileName));
}
}
private void SyncFiles()
{
string sourceDir = this.folderPath;
string destDir = this.syncFolder;
var sourcesFiles = Directory.GetFiles(sourceDir, "*.mp3");
var destFiles = Directory.GetFiles(destDir, "*.mp3");
//
// Remove extra files in destDir
//
foreach (var destFile in destFiles)
{
string fileName = Path.GetFileName(destFile);
string sourceFile = Path.Combine(sourceDir, fileName);
if (!File.Exists(sourceFile))
{
File.Delete(destFile);
}
}
//
// Copy new or updated files from sourceDir
//
foreach (var sourceFile in sourcesFiles)
{
string fileName = Path.GetFileName(sourceFile);
string destFile = Path.Combine(destDir, fileName);
bool copy = false;
if (!File.Exists(destFile))
{
copy = true;
}
else
{
FileInfo f1 = new FileInfo(sourceFile);
FileInfo f2 = new FileInfo(destFile);
if (f1.Length != f2.Length)
{
copy = true;
}
}
if (copy)
{
File.Copy(sourceFile, destFile);
}
}
}
private void ReloadFiles()
{
listViewFiles.Clear();
var files = Directory.GetFiles(this.folderPath, "*.mp3");
Array.Sort(files);
foreach (var file in files)
{
string fileName = Path.GetFileNameWithoutExtension(file);
listViewFiles.Items.Add(fileName);
}
StopPlay();
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern bool GetVolumeInformation(
string Volume,
StringBuilder VolumeName,
uint VolumeNameSize,
out uint SerialNumber,
out uint SerialNumberLength,
out uint flags,
StringBuilder fs,
uint fs_size);
private void OpenFiles(string folder)
{
this.folderPath = folder;
this.syncFolder = Path.Combine(folder, "sync");
Directory.CreateDirectory(this.syncFolder);
SyncFiles();
Directory.SetCurrentDirectory(this.folderPath);
ReloadFiles();
}
private void StartPlay()
{
var items = listViewFiles.SelectedItems;
if (items.Count != 1 || this.playing)
{
return;
}
this.playing = true;
buttonPlay.Enabled = false;
buttonStop.Enabled = true;
string fileName = items[0].Text;
string fullName = Path.Combine(this.folderPath, fileName + ".mp3");
this.player.URL = fullName;
this.player.controls.play();
}
private void StopPlay()
{
buttonPlay.Enabled = (listViewFiles.SelectedItems.Count == 1);
buttonStop.Enabled = false;
if (!this.playing)
{
return;
}
this.playing = false;
this.player.controls.stop();
}
private void saveToolStripButton_Click(object sender, EventArgs e)
{
RenameFiles();
}
private void buttonPlay_Click(object sender, EventArgs e)
{
StartPlay();
}
private void buttonStop_Click(object sender, EventArgs e)
{
StopPlay();
}
private void listViewFiles_DoubleClick(object sender, EventArgs e)
{
StartPlay();
}
private void listViewFiles_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
StopPlay();
}
private void listViewFiles_AfterLabelEdit(object sender, LabelEditEventArgs e)
{
if (e.Label == null || e.Label.Length == 0)
{
e.CancelEdit = true;
return;
}
string original = listViewFiles.Items[e.Item].Text;
string newName = e.Label;
File.Move(original + ".mp3", newName + ".mp3");
}
}
}