using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; namespace DirectoryMakerApp { public class DirectoryMaker { //public delegate string NewDelegate(string str); //NewDelegate d1 = new NewDelegate(); protected static string ProjectDirectory; protected string DestinationDirectory; protected string SourceDirectory; // public // ------------------------------------------------------------------------------------------------------------------------------------------------------ public void MakeDirectoryStructure( Dictionary ProjectInfo, Dictionary ProjectParts, Dictionary PLs, Action callbackLog) { string DirectoryName = StringCheck( ProjectInfo["PROJNO"] + "-" + ProjectInfo["CLIENT"] + "_" + ProjectInfo["PROJNAME"]); StringCheckDict(ProjectInfo); StringCheckDict(PLs); ReadConfig(); MakeMainDirectory( DestinationDirectory, DirectoryName); MakeSubDirectories( SourceDirectory, ProjectDirectory, ProjectParts); ReplaceAllProjectVaraibles( ProjectDirectory, ProjectInfo, PLs ); MakeProjectDescription( ProjectInfo, PLs); } // ------------------------------------------------------------------------------------------------------------------------------------------------------ private void MakeProjectDescription( Dictionary projectInfo, Dictionary pLs) { string path = Path.Combine(ProjectDirectory, "ProjectData.txt"); //string createText = "Hello and Welcome" + Environment.NewLine; //File.WriteAllText(path, createText); // is not in use since path is created by path combine.. if (!File.Exists(path)) { File.Create(path).Dispose(); using (TextWriter tw = new StreamWriter(path)) { tw.WriteLine("Created on " + DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm")); tw.Close(); } } if (File.Exists(path)) { using (TextWriter tw = new StreamWriter(path, true)) { foreach (KeyValuePair kvp in projectInfo) { tw.WriteLine(kvp.Key + " " + kvp.Value); } foreach (KeyValuePair kvp in pLs) { tw.WriteLine(kvp.Key + " " + kvp.Value); } } } } // protected // ------------------------------------------------------------------------------------------------------------------------------------------------------ // read config file to get source and destination protected void ReadConfig() { // get config file var cwd = Directory.GetCurrentDirectory(); Console.WriteLine("Directory path: {0}.", cwd); var configFile = Path.Combine(cwd, "config.txt"); Console.WriteLine("Directory path: {0}.", configFile); string[] lines = File.ReadAllLines(configFile); SourceDirectory = lines[0]; DestinationDirectory = lines[1]; } // ------------------------------------------------------------------------------------------------------------------------------------------------------ // create main project directory protected static void MakeMainDirectory( string DestinationDirectory, string DirectoryName) { ProjectDirectory = Path.Combine(DestinationDirectory , DirectoryName); Console.WriteLine("Directory path: {0}.", ProjectDirectory); try { // Determine whether the directory exists. if (Directory.Exists(ProjectDirectory)) { Console.WriteLine("That path exists already."); return; } // Try to create the directory. DirectoryInfo di = Directory.CreateDirectory(ProjectDirectory); Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(ProjectDirectory)); } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } finally { } } // ------------------------------------------------------------------------------------------------------------------------------------------------------ // create sub-directories protected static void MakeSubDirectories( string sourceDirectory, string projectDirectory, Dictionary projectPartsDict) { foreach (KeyValuePair PartNum in projectPartsDict) { if (PartNum.Value) { // get first char char FirstChar = PartNum.Key[0]; Console.WriteLine("Starting char: {0}.", FirstChar); // get list of dirs to copy List dirsList = GetDirsByFirstChar( sourceDirectory, FirstChar); // copy files foreach (var dir in dirsList) { DirectoryCopy( dir, projectDirectory, true, true); } } } } // ------------------------------------------------------------------------------------------------------------------------------------------------------ protected static List GetDirsByFirstChar( string ReferenceDirectory, char StartChar) { List dirsList = new List(); try { string[] dirs = Directory.GetDirectories(ReferenceDirectory, (StartChar + @"*"), SearchOption.TopDirectoryOnly); dirsList = dirs.ToList(); } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } return dirsList; } // ------------------------------------------------------------------------------------------------------------------------------------------------------ // create main project directory protected static void DirectoryCopy( string sourceDirName, string destDirName, bool SubDirs, bool copySubDirs) { // Get the subdirectories for the specified directory. DirectoryInfo dir = new DirectoryInfo(sourceDirName); // if (SubDirs) { destDirName = Path.Combine(destDirName, dir.Name); } if (!dir.Exists) { throw new DirectoryNotFoundException( "Source directory does not exist or could not be found: " + sourceDirName); } // Get the subdirectories for the specified directory. DirectoryInfo[] dirs = dir.GetDirectories(); // If the destination directory doesn't exist, create it. if (!Directory.Exists(destDirName)) { Directory.CreateDirectory(destDirName); } // Get the files in the directory and copy them to the new location. FileInfo[] files = dir.GetFiles(); foreach (FileInfo file in files) { string temppath = Path.Combine(destDirName, file.Name); file.CopyTo(temppath, false); } // If copying subdirectories, copy them and their contents to new location. if (copySubDirs) { foreach (DirectoryInfo subdir in dirs) { string temppath = Path.Combine(destDirName, subdir.Name); DirectoryCopy( subdir.FullName, temppath, false, copySubDirs); } } } // ------------------------------------------------------------------------------------------------------------------------------------------------------ protected static void StringCheckDict(Dictionary inDict) { var workDict = inDict; var keys = new List(workDict.Keys); foreach (string key in keys) { inDict[key] = StringCheck(workDict[key]); } } // ------------------------------------------------------------------------------------------------------------------------------------------------------ protected static string StringCheck(string inString) { string outString = inString; outString = Regex.Replace(outString, @" ", "_"); outString = Regex.Replace(outString, @"ä", "ae"); outString = Regex.Replace(outString, @"ö", "oe"); outString = Regex.Replace(outString, @"ü", "ue"); outString = Regex.Replace(outString, @"Ä", "Ae"); outString = Regex.Replace(outString, @"Ö", "Oe"); outString = Regex.Replace(outString, @"Ü", "Ue"); return outString; } // ------------------------------------------------------------------------------------------------------------------------------------------------------ protected static void ReplaceAllProjectVaraibles (string projectDirectory, Dictionary projectInfo, Dictionary pLs) { // Replace project number ReplaceProjectVaraible( projectDirectory, projectInfo["PROJNO"], "PROJNO"); // Replace project client ReplaceProjectVaraible( projectDirectory, projectInfo["CLIENT"], "CLIENT"); // Replace project name ReplaceProjectVaraible( projectDirectory, projectInfo["PROJNAME"], "PROJNAME"); // Replace PL 00 ReplaceProjectVaraible( projectDirectory, pLs["00"], "LEAD"); // Replace PL 10 ReplaceProjectVaraible( projectDirectory, pLs["10"], "LEADOLR"); // Replace PL 20 ReplaceProjectVaraible( projectDirectory, pLs["20"], "LEADFILM"); // Replace PL 30 ReplaceProjectVaraible( projectDirectory, pLs["30"], "LEADRT"); // Replace PL 40 ReplaceProjectVaraible( projectDirectory, pLs["40"], "LEADIWA"); // Replace PL 50 ReplaceProjectVaraible( projectDirectory, pLs["50"], "LEADDWB"); } // ------------------------------------------------------------------------------------------------------------------------------------------------------ protected static void ReplaceProjectVaraible( string projectDirectory, string projectVariable, string placeHolder) { if (projectVariable == "") { return; } // get all paths string[] FilePaths = Directory.GetFiles( projectDirectory, "*", SearchOption.AllDirectories); // replace placeholders foreach (var filePath in FilePaths) { // create new filename string NewName = Regex.Replace( filePath, @"\$" + placeHolder + @"\$", projectVariable); // rename files System.IO.File.Move( filePath, NewName); } } } }