// Insert logic for processing found files here. public static void ProcessFile(string path, FileReplace fr) { Console.WriteLine("Processed file '{0}'. ", path); string sOrigFileName = path; bool bMatch = false; foreach (Mask m in fr.aMask) { Match match = Regex.Match(path, m.sRegExp, RegexOptions.IgnoreCase); Console.WriteLine("File Name: {0} {1} match with RegExp: {2}, Index = {3}", path, (match.Success ? "" : "Does Not"), m.sRegExp, match.Index); if (match.Success == true) { bMatch = true; if (m.sAdd.Length > 0) { path = path.Insert(m.iPos + match.Index, m.sAdd); Console.WriteLine(" Add char, the new filename is {0}", path); } else { if (fr.sReplace.Length > 0) { // Console.WriteLine("The character at position {0} is a \'{1}\'", m.iPos + match.Index - 1, path[m.iPos + match.Index - 1]); path = path.Remove(m.iPos + match.Index, 1); path = path.Insert(m.iPos + match.Index, fr.sReplace); Console.WriteLine(" Replace char, the new filename is {0}", path); } else { // Console.WriteLine("The character at position {0} is a \'{1}\'", m.iPos + match.Index - 1, path[m.iPos + match.Index - 1]); // path = path.Remove(m.iPos + match.Index, 1); path = path.Insert(m.iPos + match.Index, fr.sInsert); Console.WriteLine(" Insert string, the new filename is {0}", path); } } } } if (bMatch == true) { if (File.Exists(path)) { // throw new ArgumentOutOfRangeException("File Exsists, cannot change name"); ; Console.WriteLine(" The file {0} exsists, skipping replace.", path); } else { File.Move(sOrigFileName, path); } } }
public static void ProcessDirectory(string targetDirectory, FileReplace fr) { // Process the list of files found in the directory. string[] fileEntries = Directory.GetFiles(targetDirectory); foreach (string fileName in fileEntries) { ProcessFile(fileName, fr); } if (fr.bSubDir == true) { // Recurse into subdirectories of this directory. string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory); foreach (string subdirectory in subdirectoryEntries) { ProcessDirectory(subdirectory, fr); } } }
static void Main(string[] args) { //Declarations string sGNU = "\n\nModify Photo Number\nVersion 1.01, 3-Jan-2016\n\nFor assistance contact Larry Young at [email protected].\n\nThis software is copyrighted by Larry N. Young, 2016. \nThere is no warranty for this software. A copy of the full \nlicence is included in the\nsource code and at http://www.gnu.org/licenses/gpl.txt\n\n"; Console.WriteLine("{0}", sGNU); string sFilePath = "PhotoPath.dat"; //Add the file name to the path FileInfo existingFile = new FileInfo(sFilePath); //Code if (existingFile.Exists == false) { Console.WriteLine("\n\nFATAL ERROR: the PhotoPath.dat file in the program directory does not exsist.\nFind a copy of the file and put in the proper directory. Exiting..."); Console.Write("\n\nPress any key"); Console.ReadLine(); Environment.Exit(2); } StreamReader srFile = null; try { //Open the file, throws an exception when file not found srFile = new StreamReader(sFilePath); } catch { Console.WriteLine("\nThe {0} file did not open properly, exiting....\n", sFilePath); Console.Write("\n\nPress any key"); Console.ReadLine(); return; } ArrayList sConfigData = new ArrayList(); int iCount = 0; sConfigData.Add(srFile.ReadLine()); while (!srFile.EndOfStream) { Console.WriteLine("Line {0}: {1}", iCount, sConfigData[iCount]); sConfigData.Add(srFile.ReadLine()); iCount++; } //Read last line Console.WriteLine("Line {0}: {1}", iCount, sConfigData[iCount]); sConfigData.Add(srFile.ReadLine()); iCount++; Console.WriteLine("\nThere were {0} lines of text read ", iCount); srFile.Close(); StructData[] sd = new StructData[iCount]; for (int i = 0; i < iCount; i++) { sd[i] = ParseConfigData((string)sConfigData[i]); } Console.WriteLine("\nThe Photo Data path is: {0}\n", sd[0].sValue); try { FileReplace fr = ProcessConfigData(sd); foreach (StructData s in sd) { if (s.sKey == "Path") { if (Directory.Exists(s.sValue)) { ProcessDirectory(s.sValue, fr); } else { Console.WriteLine("\nThe directory {0} does not exsist and was not processed.\n", s.sValue); } } } } catch (ArgumentOutOfRangeException ex) { Console.WriteLine("ERROR: exception thrown, value ={0}", ex); Environment.Exit(2); } }
// Process all files in the directory passed in, recurse on any directories // that are found, and process the files they contain. public static FileReplace ProcessConfigData(StructData[] sd) { FileReplace fr = new FileReplace(); fr.aMask = new List <Mask>(); fr.saPath = new List <string>(); fr.sReplace = ""; foreach (StructData s in sd) { switch (s.sKey) { case "Mask": Console.WriteLine("Mask: {0}", s.sValue); fr.aMask.Add(BuildRegExp(s.sValue)); break; case "Path": Console.WriteLine("Path: {0}", s.sValue); fr.saPath.Add(s.sValue); break; case "Replace": Console.WriteLine("Insert: {0}", s.sValue); fr.sReplace = s.sValue; break; case "Insert": Console.WriteLine("Replace: {0}", s.sValue); fr.sInsert = s.sValue; break; case "SubDir": Console.WriteLine("SubDir: {0}", s.sValue); if (s.sValue == "Include") { fr.bSubDir = true; } else { fr.bSubDir = false; } break; default: Console.WriteLine("No key match, sValue = {0}", s.sValue); break; } } if (fr.aMask[0].sRegExp.Length < 1) { throw new ArgumentOutOfRangeException("Mask"); } /* foreach (Mask m in fr.aMask) * { * if (m.iPos != -1 && m.cAddChar == '\0') * throw new ArgumentOutOfRangeException("MissingInsertValue"); * } */ return(fr); }