1: using System;
2: using System.Collections.Generic;
3: using System.ComponentModel;
4: using System.Data;
5: using System.Drawing;
6: using System.Text;
7: using System.Windows.Forms;
8: using System.Text.RegularExpressions;
9: using System.IO;
10:
11: namespace RenameFiles
12: { 13: public partial class Form1 : Form
14: { 15: public Form1()
16: { 17: InitializeComponent();
18: button2.Enabled = false;
19: }
20:
21: private Regex FileNameRegex;
22: private void button1_Click(object sender, EventArgs e)
23: { 24: openFileDialog1.ShowDialog();
25: }
26:
27:
28: private FileInfo FirstFile;
29: private Match FileNameMatch;
30: private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
31: { 32: if (!e.Cancel)
33: { 34: OpenFileDialog FirstFileDlg = sender as OpenFileDialog;
35: FirstFile = new FileInfo(FirstFileDlg.FileName);
36: FileNameRegex = new Regex(textBox1.Text, RegexOptions.Compiled | RegexOptions.IgnoreCase);
37: FileNameMatch = FileNameRegex.Match(FirstFile.Name);
38: if (FileNameMatch.Success)
39: { 40: textBox2.Text = FileNameMatch.Groups["NAME"].Value;
41: numericUpDown1.Value = Convert.ToInt32(FileNameMatch.Groups["INDEX"].Value);
42: textBox3.Text = FileNameMatch.Groups["EXT"].Value;
43:
44:
45: ChangeLabel();
46:
47: button2.Enabled = true;
48: numericUpDown2.Enabled = true;
49: numericUpDown1.Enabled = true;
50: numericUpDown3.Enabled = true;
51: numericUpDown3.Value = numericUpDown1.Value;
52: checkBox1.Enabled = true;
53: textBox2.Enabled = true;
54: textBox3.Enabled = true;
55: }
56: else
57: { 58: MessageBox.Show("Selected File doesn't match Regular Expression!"); 59: }
60: }
61: }
62:
63: private void button2_Click(object sender, EventArgs e)
64: { 65: FileNameRegex = new Regex(textBox1.Text, RegexOptions.Compiled | RegexOptions.IgnoreCase);
66: DirectoryInfo Folder = FirstFile.Directory;
67:
68: Match m;
69: SortedDictionary<int, FileInfo> MatchedFiles = new SortedDictionary<int, FileInfo>();
70:
71: int Index = 0;
72: int MinIndex = (int)numericUpDown1.Value + 9999;
73: int MaxIndex = 0;
74: int ShiftIndexBy = (int)numericUpDown2.Value;
75:
76: foreach (FileInfo f in Folder.GetFiles())
77: { 78:
79: //Match file names
80: m = FileNameRegex.Match(f.Name);
81:
82: if (m.Success)
83: { 84: Index = Convert.ToInt32(m.Groups["INDEX"].Value);
85:
86: if (Index >= numericUpDown1.Value && Index <= numericUpDown3.Value)
87: { 88: try
89: { 90: MatchedFiles.Add(Index, f);
91: if (Index <= MinIndex) MinIndex = Index;
92: if (Index >= MaxIndex) MaxIndex = Index;
93: }
94: catch (Exception Ex)
95: { 96: MessageBox.Show(Ex.Message + "\n" + "Another match for "
97: + f.Name + " already exists in file list!", "Error while filtering files",
98: MessageBoxButtons.OK, MessageBoxIcon.Error);
99: }
100: }
101: }
102:
103: }
104:
105: MessageBox.Show(MatchedFiles.Count.ToString() + " files found\n" +
106: "First file index " + MinIndex.ToString() + "\n" +
107: "Last file index " + MaxIndex.ToString(), "Renaming files...",
108: MessageBoxButtons.OK, MessageBoxIcon.Information);
109:
110: if((MatchedFiles.Count == 0) || ( ShiftIndexBy == 0
111: && textBox2.Text == FileNameMatch.Groups["NAME"].Value
112: && checkBox1.Checked == true
113: && textBox3.Text == FileNameMatch.Groups["EXT"].Value ))
114: { 115: MessageBox.Show("There is no file to be renamed. DONE!"); 116: }
117:
118: if (MatchedFiles.Count > 0 && ShiftIndexBy <= 0)
119: { 120: FileInfo fi;
121: for (int i = MinIndex; i <= MaxIndex; i++ )
122: { 123: if (MatchedFiles.TryGetValue(i, out fi))
124: { 125: string NewFileName = textBox2.Text
126: + (checkBox1.Checked ? String.Format("{0:0000}", i 127: + numericUpDown2.Value) : String.Format("{0}", i 128: + numericUpDown2.Value)) + textBox3.Text;
129: try
130: { 131: fi.MoveTo(NewFileName);
132: }
133: catch (Exception Ex)
134: { 135: MessageBox.Show(Ex.Message + "\n" + "Attempt to rename "
136: + fi.Name + " to " + NewFileName + " failed!",
137: "Error while moving files", MessageBoxButtons.OK, MessageBoxIcon.Error);
138: }
139: }
140: }
141: }
142:
143:
144:
145: if (MatchedFiles.Count > 0 && ShiftIndexBy > 0)
146: { 147: FileInfo fi;
148: for (int i = MaxIndex; i >= MinIndex; i--)
149: { 150: if (MatchedFiles.TryGetValue(i, out fi))
151: { 152: string NewFileName = textBox2.Text
153: + (checkBox1.Checked ? String.Format("{0:0000}", i 154: + numericUpDown2.Value) : String.Format("{0}", i 155: + numericUpDown2.Value)) + textBox3.Text;
156: try
157: { 158: fi.MoveTo(NewFileName);
159: }
160: catch (Exception Ex)
161: { 162: MessageBox.Show(Ex.Message + "\n" + "Attempt to rename "
163: + fi.Name + " to " + NewFileName + " failed!",
164: "Error while moving files", MessageBoxButtons.OK,
165: MessageBoxIcon.Error);
166: }
167: }
168: }
169: }
170: }
171:
172: private void numericUpDown1_ValueChanged(object sender, EventArgs e)
173: { 174: ChangeLabel();
175: }
176:
177: private void ChangeLabel()
178: { 179: label1.Text = "Renames all files in a folder from e.g. \""
180: + FileNameMatch.Groups["NAME"].Value
181: + String.Format("{0:0000}", (int)numericUpDown1.Value) 182: + FileNameMatch.Groups["EXT"].Value + "\" to \""
183: + (checkBox1.Checked ? textBox2.Text
184: + String.Format("{0:0000}", (Convert.ToInt32(FileNameMatch.Groups["INDEX"].Value) 185: + numericUpDown2.Value)) : textBox2.Text
186: + String.Format("{0}", (Convert.ToInt32(FileNameMatch.Groups["INDEX"].Value) 187: + numericUpDown2.Value))) + textBox3.Text +"\"";
188: }
189:
190: private void numericUpDown2_ValueChanged(object sender, EventArgs e)
191: { 192: ChangeLabel();
193: }
194:
195: private void textBox2_TextChanged(object sender, EventArgs e)
196: { 197: ChangeLabel();
198: }
199:
200: private void checkBox1_CheckedChanged(object sender, EventArgs e)
201: { 202: ChangeLabel();
203: }
204:
205: private void textBox3_TextChanged(object sender, EventArgs e)
206: { 207: ChangeLabel();
208: }
209:
210: private void Form1_Load(object sender, EventArgs e)
211: { 212:
213: }
214:
215: private void numericUpDown3_ValueChanged(object sender, EventArgs e)
216: { 217: ChangeLabel();
218: }
219:
220:
221: }
222: }