private static void Main(string[] args) { //Use the args passed to the program to build path references var p = new ProgramPaths(args[0]); //Exit program if the file doesn't exist if (!File.Exists(p.OriginalFullPath)) { return; } //Check for the working directory. Delete all files and directories //if it exists, otherwise create the directory. if (p.WorkingDirectory.Exists) { p.WorkingDirectory.ClearAll(); } else { p.WorkingDirectory.Create(); } //Unzip the original file contents to the working directory. UnzipFilesToWorkingDir(p.OriginalFullPath, p.WorkingDirectory); //Merge the files in the working directory together into one file MergeFiles(p); //Move the file back to the original file location overwritting the original File.Copy(Path.Combine(p.WorkingDirectory.FullName, p.OriginalFileName), p.OriginalFullPath, true); //Clear the working directory p.WorkingDirectory.ClearAll(); }
private static void MergeFiles(ProgramPaths p) { //Get the list of files from the working directory var fileList = Directory.GetFiles(p.WorkingDirectory.FullName); //Create a stream to the output file using (var outFile = new StreamWriter(Path.Combine(p.WorkingDirectory.FullName, p.OriginalFileName), false)) { //Keep track of files to know when the last file is being worked on var counter = fileList.Length; //Read through each file checking for summary records to be discarded before writing the output foreach (var file in fileList) { //If this is the last file remove the last form feed line from the data before writing out. if (counter != 1) { WriteToFile(ParseFile(file), outFile); } else { var holder = ParseFile(file); holder.RemoveAt(holder.Count - 1); WriteToFile(holder, outFile); } counter--; } } }