public static void AddNumbers() { FileNumbering app = new FileNumbering(); string directoryPath = @"C:\Users\Ralph\Documents\test\"; DirectoryInfo directory = new DirectoryInfo(directoryPath); FileInfo[] files = directory.GetFiles(); int count = 0; int arrayLenght = files.Length; foreach (FileInfo element in files) { count += 1; string s = Convert.ToString(count); if (arrayLenght > 98) { if (count < 10) { File.Move(directoryPath + element, directoryPath + "00" + s + " " + element); //Console.WriteLine(directoryPath + tempString); } else if (count < 100) { File.Move(directoryPath + element, directoryPath + "0" + s + " " + element); //Console.WriteLine(directoryPath + tempString); } else { File.Move(directoryPath + element, directoryPath + s + " " + element); } //Console.WriteLine("file {0}: {1}", count, element); } else { if (count < 10) { File.Move(directoryPath + element, directoryPath + "0" + s + " " + element); //Console.WriteLine(directoryPath + tempString); } else { File.Move(directoryPath + element, directoryPath + s + " " + element); //Console.WriteLine(directoryPath + tempString); } } } }
private void processButton_Click(object sender, RoutedEventArgs e) { //Number first then do the find replace. That will ensure that things are kept in the same order of the original list if (numberCheckBox.IsChecked.ToString() == "True" && removeNumberCheckBox.IsChecked.ToString() == "False") { FileNumbering.AddNumbers(); } else if (numberCheckBox.IsChecked.ToString() == "False" && removeNumberCheckBox.IsChecked.ToString() == "True") { FileNumbering.RemoveNumbers(); } //This does checks to ensure that a destination folder is selected and that there is some text to find if ((pathBox.Text != "" || pathBox.Text != @"ex: c:\users\default\documents\") && findTextBox.Text != "") { int charAfter, charBefore; // converts the characters before and characters after boxes in the form to integers to be used //in computing index positions in the follow on method. if it's left blank then it just sets the //value to 0. otherwise it's the integer value of what was typed in if (charactersAfterTextBox.Text == "") { charAfter = 0; } else { charAfter = Int32.Parse(charactersAfterTextBox.Text); } if (charactersBeforeTextBox.Text == "") { charBefore = 0; } else { charBefore = Int32.Parse(charactersBeforeTextBox.Text); } //this is a call to actually do the find and replace given all the pertinent parameters SearchClass.FindReplace(pathBox.Text, findTextBox.Text, replaceTextBox.Text, charBefore, charAfter); } else { MessageBox.Show("You have to select a folder or the program will crash"); } }
public static void RemoveNumbers() { FileNumbering app = new FileNumbering(); string directoryPath = @"C:\Users\Ralph\Documents\test\"; DirectoryInfo directory = new DirectoryInfo(directoryPath); FileInfo[] files = directory.GetFiles(); int count = 0; int arrayLenght = files.Length; string tempString = null; foreach (FileInfo element in files) { count += 1; string s = Convert.ToString(count); tempString = element.ToString(); if (arrayLenght > 98) { if (count < 10) { app.searchFor = "00" + s + " "; app.replaceWith = ""; tempString = Regex.Replace(tempString, app.searchFor, app.replaceWith, RegexOptions.IgnoreCase); File.Move(directoryPath + element, directoryPath + tempString); //Console.WriteLine(directoryPath + tempString); } else if (count < 100) { app.searchFor = "0" + s + " "; app.replaceWith = ""; tempString = Regex.Replace(tempString, app.searchFor, app.replaceWith, RegexOptions.IgnoreCase); File.Move(directoryPath + element, directoryPath + tempString); //Console.WriteLine(directoryPath + tempString); } else { app.searchFor = s + " "; app.replaceWith = ""; tempString = Regex.Replace(tempString, app.searchFor, app.replaceWith, RegexOptions.IgnoreCase); File.Move(directoryPath + element, directoryPath + tempString); //Console.WriteLine(directoryPath + tempString); } } else { if (count < 10) { app.searchFor = "0" + s + " "; app.replaceWith = ""; tempString = Regex.Replace(tempString, app.searchFor, app.replaceWith, RegexOptions.IgnoreCase); File.Move(directoryPath + element, directoryPath + tempString); //Console.WriteLine(directoryPath + tempString); } else { app.searchFor = s + " "; app.replaceWith = ""; tempString = Regex.Replace(tempString, app.searchFor, app.replaceWith, RegexOptions.IgnoreCase); File.Move(directoryPath + element, directoryPath + tempString); //Console.WriteLine(directoryPath + tempString); } } //Console.WriteLine("file {0}: {1}", count, element); } }