-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrmMain.cs
655 lines (546 loc) · 22 KB
/
FrmMain.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
using System;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace brf
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
#region var section
// public vars
public string globFolder;
public FileInfo[] globFileTypes;
public SearchOption SearchOptionUserType;
// public constants
public const string vbCrl = "\n";
public const string backslash = "\\";
// file type files supported
public string filetypeOffice = ConfigurationManager.AppSettings["filetypeOffice"];
public string filetypeOfficex = ConfigurationManager.AppSettings["filetypeOfficex"];
public string filetypeCompressed = ConfigurationManager.AppSettings["filetypeCompressed"];
public string filetypeAudio = ConfigurationManager.AppSettings["filetypeAudio"];
public string filetypeVideo = ConfigurationManager.AppSettings["filetypeVideo"];
public string filetypeData = ConfigurationManager.AppSettings["filetypeData"];
public string filetypeDisk = ConfigurationManager.AppSettings["filetypeDisk"];
public string filetypeExecutable = ConfigurationManager.AppSettings["filetypeExecutable"];
public string filetypeImage = ConfigurationManager.AppSettings["filetypeImage"];
public string filetypeText = ConfigurationManager.AppSettings["filetypeText"];
public string filetypeEverythin = ConfigurationManager.AppSettings["filetypeEverythin"];
public string supportedFiletype;
#endregion
/// <summary>
/// reset values from folder and file forms
/// </summary>
private void ResetFolderAndFiles()
{
chkbFilesDocs.Checked = false;
chkbFilesZipped.Checked = false;
chkbFilesAudio.Checked = false;
chkbFilesVideo.Checked = false;
chkbFilesData.Checked = false;
chkbFilesDisk.Checked = false;
chkbFilesExe.Checked = false;
chkbFilesImg.Checked = false;
chkbFilesTxt.Checked = false;
chkbFilesAll.Checked = false;
chkbFilesOther.Checked = false;
chksubfolders.Checked = false;
chksubfolders.Enabled = true;
globFolder = string.Empty;
lblFolderSelected.Text = "[...]";
lblFolderSelected.ForeColor = System.Drawing.Color.Black;
lvFilesFolders.Items.Clear();
}
/// <summary>
/// reset values from what and where forms
/// </summary>
private void resetWhatandWhere()
{
txtReplaceThis.Text = string.Empty;
txtReplaceForThis.Text = string.Empty;
rbjustremove.Checked = false;
rbregexWhatAndWhere.Checked = false;
chkbFilesRegex.Checked = false;
chkOpenFE.Checked = false;
lvFinalPrev.Clear();
}
private void frmMain_Load(object sender, EventArgs e)
{
_ = txtbFileTypes.Focus();
}
/// <summary>
/// Enable the option to enter manually what type of files to read
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chkbFilesOther_CheckedChanged(object sender, EventArgs e)
{
if ((chkbFilesOther.Checked) && String.IsNullOrEmpty(txtbFileTypes.Text))
{
txtbFileTypes.ReadOnly = false;
_ = txtbFileTypes.Focus();
}
else
{
txtbFileTypes.Text = String.Empty;
txtbFileTypes.ReadOnly = true;
}
}
/// <summary>
/// Display a preview: of how the files going to be once finished the process
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPreview_Click(object sender, EventArgs e)
{
int counter = 0;
string finalName = string.Empty;
string filename = string.Empty;
string whatToFind = string.Empty;
string replaceForThis = string.Empty;
lvFinalPrev.Items.Clear();
lvFinalPrev.Scrollable = true;
lvFinalPrev.View = View.Details;
lvFinalPrev.HeaderStyle = ColumnHeaderStyle.None;
if(supportedFiletype == "*.*")
{
// get file list from supported file types selected
foreach(var supportedFile in Directory.GetFiles(globFolder, "*.*", SearchOptionUserType))
{
filename = string.Empty;
finalName = string.Empty;
filename = System.IO.Path.GetFileName(supportedFile).ToLower();
whatToFind = txtReplaceThis.Text.ToLower();
if(filename.Contains(whatToFind))
{
counter += 1;
replaceForThis = txtReplaceForThis.Text;
finalName = filename.Replace(whatToFind, replaceForThis);
Console.WriteLine("File Original Name : {0}", System.IO.Path.GetFileName(filename));
Console.WriteLine("File renamed Name : {0}", System.IO.Path.GetFileName(finalName));
lvFinalPrev.Items.Add(finalName);
}
}
}
else
{
// get file list from supported file types selected
foreach (var supportedFile in Directory.GetFiles(globFolder, "*.*", SearchOptionUserType).Where(s => supportedFiletype.Contains(Path.GetExtension(s).ToLower())))
{
filename = string.Empty;
finalName = string.Empty;
filename = System.IO.Path.GetFileName(supportedFile).ToLower();
whatToFind = txtReplaceThis.Text.ToLower();
if (filename.Contains(whatToFind))
{
counter += 1;
replaceForThis = txtReplaceForThis.Text;
finalName = filename.Replace(whatToFind, replaceForThis);
Console.WriteLine("File Original Name : {0}", System.IO.Path.GetFileName(filename));
Console.WriteLine("File renamed Name : {0}", System.IO.Path.GetFileName(finalName));
lvFinalPrev.Items.Add(finalName);
}
}
}
if (counter >= 1)
{
lvFinalPrev.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}
else
MessageBox.Show(counter.ToString() + " files found " + vbCrl + "...for '" + whatToFind + "'", "Something went wrong: no files found!", MessageBoxButtons.OK, MessageBoxIcon.Information);
//SaveLogToFile("", "Component.Testing.CRUD", "Previsualizando", "uno:asdasd;dos:dfgdfgdfg;tres:dfgdfgd");
}
/// <summary>
/// the renaming process
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSubmit_Click(object sender, EventArgs e)
{
int counter = 0;
string finalName = string.Empty;
string filename = string.Empty;
try
{
// look for files
foreach (var supportedFile in Directory.GetFiles(globFolder, "*.*", SearchOptionUserType).Where(s => supportedFiletype.Contains(Path.GetExtension(s).ToLower())))
{
if (supportedFile.ToLower().Contains(txtReplaceThis.Text.ToLower()))
{
counter += 1;
filename = string.Empty;
finalName = string.Empty;
filename = System.IO.Path.GetFileName(supportedFile);
statusStrip.Text = "...getting supported files.";
finalName = filename.ToLower().Replace(txtReplaceThis.Text.ToLower(), txtReplaceForThis.Text.ToLower());
statusStrip.Text = "...getting file name.";
Console.WriteLine("File original Name : {0}", filename);
Console.WriteLine("File renamed Name : {0}", finalName);
// rename (move) file!!!
string originalFileName = string.Empty;
string newFileName = string.Empty;
// determine path and file name: original
originalFileName = globFolder + backslash + filename;
// determine path and file name: new
newFileName = globFolder + backslash + finalName;
// exist file?
if (File.Exists(originalFileName))
{
// jusp procces if the file doesn't exist
if (originalFileName != newFileName)
{
File.Move(originalFileName, newFileName);
statusStrip.Text = "...renaming file.";
}
}
}
}
// do it open file explorer?
if (chkOpenFE.Checked)
{
// open folder in file explorer once the proccess it's done
Process.Start(globFolder);
}
MessageBox.Show("The process renamed " + counter.ToString() + " files!" + vbCrl + "...Everything was like a globe!", "Success: files renamed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (IOException ioEx)
{
// exception for Directory.GetFiles and File.move objects
Console.WriteLine(ioEx.Message);
MessageBox.Show("Error on renaming files: " + vbCrl + ioEx.Message.ToString(), "File system Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Win32Exception w32Exce)
{
// exception for Process.Start
Console.WriteLine(w32Exce.Message);
MessageBox.Show("Error to open File Explorer: " + vbCrl + w32Exce.Message.ToString(), "Something went wrong...", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
// all other type of exceptions
Console.WriteLine(ex.Message);
MessageBox.Show("Error on renaming files: " + vbCrl + ex.Message.ToString(), "Other error type: ", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// set up whether must use sub folder option (cascade file read)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chksubfolders_CheckedChanged(object sender, EventArgs e)
{
if (chksubfolders.Enabled)
SearchOptionUserType = SearchOption.AllDirectories;
else
SearchOptionUserType = SearchOption.TopDirectoryOnly;
}
private void btnResetFileFolder_Click(object sender, EventArgs e)
{
ResetFolderAndFiles();
}
private void btnResetWhatTo_Click(object sender, EventArgs e)
{
resetWhatandWhere();
}
private void btnResetAll_Click(object sender, EventArgs e)
{
ResetFolderAndFiles();
resetWhatandWhere();
}
/// <summary>
/// Determine what type of file need to look for
/// </summary>
/// <returns>string with the type of files to look for</returns>
private string WhatTypeOfFiles()
{
if (chkbFilesDocs.Checked)
if (string.IsNullOrEmpty(supportedFiletype))
supportedFiletype = filetypeOffice + ", " + filetypeOfficex;
else
supportedFiletype += ", " + filetypeOffice + ", " + filetypeOfficex;
if (chkbFilesZipped.Checked)
if (string.IsNullOrEmpty(supportedFiletype))
supportedFiletype += filetypeCompressed; //
else
supportedFiletype += ", " + filetypeCompressed;
if (chkbFilesAudio.Checked)
if (string.IsNullOrEmpty(supportedFiletype))
supportedFiletype += filetypeAudio; //
else
supportedFiletype += ", " + filetypeAudio;
if (chkbFilesVideo.Checked)
if (string.IsNullOrEmpty(supportedFiletype))
supportedFiletype += filetypeVideo; //
else
supportedFiletype += ", " + filetypeVideo;
if (chkbFilesData.Checked)
if (string.IsNullOrEmpty(supportedFiletype))
supportedFiletype += filetypeData; //
else
supportedFiletype += ", " + filetypeVideo;
if (chkbFilesDisk.Checked)
if (string.IsNullOrEmpty(supportedFiletype))
supportedFiletype += filetypeDisk; //
else
supportedFiletype += ", " + filetypeDisk;
if (chkbFilesExe.Checked)
if (string.IsNullOrEmpty(supportedFiletype))
supportedFiletype += filetypeExecutable; //
else
supportedFiletype += ", " + filetypeExecutable;
if (chkbFilesImg.Checked)
if (string.IsNullOrEmpty(supportedFiletype))
supportedFiletype = filetypeImage;
else
supportedFiletype += ", " + filetypeImage;
if (chkbFilesTxt.Checked)
if (string.IsNullOrEmpty(supportedFiletype))
supportedFiletype = filetypeText;
else
supportedFiletype += ", " + filetypeText;
if (chkbFilesAll.Checked)
{
if (string.IsNullOrEmpty(supportedFiletype))
supportedFiletype = filetypeEverythin;
else
supportedFiletype += ", " + filetypeEverythin;
}
return supportedFiletype;
}
/// <summary>
/// main process
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSelectFolder_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbdResult = new FolderBrowserDialog();
int counter = 0;
// disable sub folders option once event is started
if (!chksubfolders.Checked)
{
SearchOptionUserType = SearchOption.TopDirectoryOnly;
chksubfolders.Enabled = false;
}
supportedFiletype = string.Empty;
// What type of files must be included?
supportedFiletype = WhatTypeOfFiles();
#region evaluate if there's some selected, related to file type
// check if have any file type to read
if (string.IsNullOrEmpty(supportedFiletype))
{
MessageBox.Show("Select or Write some file type!", "Atention!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
_ = chkbFilesDocs.Focus();
return;
}
// This section: gathers information about what file types must look for
if ((chkbFilesOther.Checked == true) &&
(txtbFileTypes.ReadOnly == false) &&
(String.IsNullOrEmpty(txtbFileTypes.Text)))
{
MessageBox.Show("Write some file type!", "Atention!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
_ = txtbFileTypes.Focus();
return;
}
#endregion
// select folder to work
if (fbdFolder.ShowDialog() == DialogResult.OK)
{
lblFolderSelected.Text = fbdFolder.SelectedPath.ToString();
globFolder = fbdFolder.SelectedPath.ToString();
lblFolderSelected.ForeColor = System.Drawing.Color.Navy;
}
else
return;
lvFilesFolders.Items.Clear();
// set up folder
DirectoryInfo dir = new DirectoryInfo(globFolder);
lvFilesFolders.Scrollable = true;
lvFilesFolders.View = View.Details;
lvFilesFolders.HeaderStyle = ColumnHeaderStyle.None;
if(supportedFiletype == "*.*")
{
foreach(var supportedFile in Directory.GetFiles(globFolder, "*.*", SearchOptionUserType))
{
counter += 1;
Console.WriteLine("File Name : {0}", System.IO.Path.GetFileName(supportedFile));
lvFilesFolders.Items.Add(System.IO.Path.GetFileName(supportedFile));
}
}
else
{
// get file list from supported file types selected
foreach (var supportedFile in Directory.GetFiles(globFolder, "*.*", SearchOptionUserType).Where(s => supportedFiletype.Contains(Path.GetExtension(s).ToLower())))
{
counter += 1;
Console.WriteLine("File Name : {0}", System.IO.Path.GetFileName(supportedFile));
lvFilesFolders.Items.Add(System.IO.Path.GetFileName(supportedFile));
}
}
if (counter <= 0)
{
MessageBox.Show("No supported files found!", "Atention!", MessageBoxButtons.OK, MessageBoxIcon.Information);
_ = txtbFileTypes.Focus();
return;
}
else
{
lblFolderSelected.Text += " (" + counter.ToString() + " files found)";
lvFilesFolders.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
_ = txtReplaceThis.Focus();
}
SaveLogToFile("", "NONE", "Acción: Determina carpeta y archivos a buscar.", "param 01: 12345;param 2: 3456456;param 3: 5675675;");
}
private void lblwtr_Click(object sender, EventArgs e)
{
}
private void chkbFilesAll_CheckedChanged(object sender, EventArgs e)
{
if (chkbFilesAll.Checked)
{
chkbFilesDocs.Checked = false;
chkbFilesZipped.Checked = false;
chkbFilesAudio.Checked = false;
chkbFilesVideo.Checked = false;
chkbFilesData.Checked = false;
chkbFilesDisk.Checked = false;
chkbFilesExe.Checked = false;
chkbFilesImg.Checked = false;
chkbFilesTxt.Checked = false;
//chkbFilesAll.Checked = false;
chkbFilesOther.Checked = false;
}
}
private void toolStripStatusLabel1_Click(object sender, EventArgs e)
{
}
private void chkbFilesDocs_MouseHover(object sender, EventArgs e)
{
statusStrip.Text = "docs!";
}
private void chkbFilesDocs_MouseLeave(object sender, EventArgs e)
{
statusStrip.Text = string.Empty;
}
private void rdbReplaceThis_CheckedChanged(object sender, EventArgs e)
{
if (rdbReplaceThis.Checked) {
txtReplaceThis.Enabled = true;
txtReplaceForThis.Enabled = true;
rbregexWhatAndWhere.Checked = false;
rbjustremove.Checked = false;
txtReplaceThis.Focus();
}
else {
txtReplaceThis.Enabled = false;
txtReplaceForThis.Enabled = false;
}
}
private void rbregexWhatAndWhere_CheckedChanged(object sender, EventArgs e)
{
if (rbregexWhatAndWhere.Checked)
{
rdbReplaceThis.Checked = false;
rbjustremove.Checked = false;
txtbRegexWAndW.ReadOnly = false;
rbregexWhatAndWhere.Focus();
}
else
{
txtbRegexWAndW.Text = string.Empty;
txtbRegexWAndW.ReadOnly = true;
}
}
private void rbjustremove_CheckedChanged(object sender, EventArgs e)
{
if (rbjustremove.Checked)
{
rdbReplaceThis.Checked = false;
rbregexWhatAndWhere.Checked = false;
txtJustDropIt.Enabled = true;
txtJustDropIt.Focus();
}
else
{
txtJustDropIt.Enabled = false;
}
}
/// <summary>
///
/// </summary>
/// <param name="strSource"></param>
/// <param name="strStart"></param>
/// <param name="strEnd"></param>
/// <returns></returns>
public static string getBetween(string strSource, string strStart, string strEnd)
{
//from Stackoverflow
// https://stackoverflow.com/questions/10709821/find-text-in-string-with-c-sharp
if (strSource.Contains(strStart) && strSource.Contains(strEnd))
{
int Start, End;
Start = strSource.IndexOf(strStart, 0) + strStart.Length;
End = strSource.IndexOf(strEnd, Start);
return strSource.Substring(Start, End - Start);
}
return "";
}
/// <summary>
/// Save log to file
/// </summary>
/// <param name="pathAndFileLog">path and file name log</param>
/// <param name="componentName">Componente name used (Send "NONE" to avoid this parameter)</param>
/// <param name="isAction">What is doing this action (Describe)</param>
/// <param name="thisParams">List of what to print with semicolon separator. (param1:value1;param2:value2...)</param>
public void SaveLogToFile(string pathAndFileLog, string componentName, string isAction, string thisParams)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string[] splitedParams;
string printToFile = "";
int counter = 0;
printToFile += "\r\n";
printToFile += "\r\n";
printToFile = DateTime.Now.ToString("yyyyMMddhhmmss") + " ********************************************";
printToFile += "\r\n";
if (componentName != "NONE" )
{
printToFile += DateTime.Now.ToString("yyyyMMddhhmmss") + " * " + componentName;
printToFile += "\r\n";
}
printToFile += DateTime.Now.ToString("yyyyMMddhhmmss") + " ********************************************";
printToFile += "\r\n";
printToFile += DateTime.Now.ToString("yyyyMMddhhmmss") + " * " + isAction;
printToFile += "\r\n";
printToFile += DateTime.Now.ToString("yyyyMMddhhmmss") + " * " + "Parameters: ";
printToFile += "\r\n";
splitedParams = thisParams.Split(';');
foreach (string valpar in splitedParams)
{
counter += 1;
printToFile += DateTime.Now.ToString("yyyyMMddhhmmss") + " * " + valpar;
printToFile += "\r\n";
}
if (string.IsNullOrEmpty(pathAndFileLog))
{
FileInfo fileInfo = new FileInfo("c:\\skidata\\logs\\");
if (!fileInfo.Exists)
Directory.CreateDirectory(fileInfo.Directory.FullName);
pathAndFileLog += "c:\\skidata\\logs\\" + DateTime.Now.ToString("yyyyMMdd") + ".log";
sb.Append(printToFile);
File.AppendAllText(pathAndFileLog, sb.ToString());
}
else
{
File.AppendAllText(pathAndFileLog, printToFile + Environment.NewLine);
}
sb.Clear();
}
}
}