/// <summary> /// process the files provided and output the results /// </summary> /// <param name="sourceFile">the file that contains the data to process</param> /// <param name="delimiter">the file delimiter</param> /// <param name="sourceFileFieldCount">the number of fields in the file</param> private static void ProcessFile(string sourceFile, InputValidator.FileDelimiter delimiter, int sourceFileFieldCount) { char spliter = ToDelimiter(delimiter); //keep track of if we actual wrote data to a file bool validFileCreated = false; bool inValidFileCreated = false; IEnumerable <string> results = FileIO.Read(sourceFile, true); //write the rows that are valid to a file (has the correct number of fields) validFileCreated = FileIO.Write(UIMessages.FilePath_ValidData, results.Where(x => x.Split(spliter).Length == sourceFileFieldCount)); //write the rows that are invalid to a file (does the have the correct number of fields) inValidFileCreated = FileIO.Write(UIMessages.FilePath_InValidData, results.Where(x => x.Split(spliter).Length != sourceFileFieldCount)); PrintMessage(UIMessages.MessageProcessingComplete); //if we wrote data to a file display the file name and path if (validFileCreated | inValidFileCreated) { PrintMessage(UIMessages.MessageOutputLocation, Path.GetDirectoryName(UIMessages.FilePath_ValidData)); } else { PrintMessage(UIMessages.MessageEmptyFile); //no data was written let the user know } }
/// <summary> /// Convert the FileDelimiter enum to an acutal delimiter /// </summary> /// <param name="value">Enum to convert to a delimiter</param> /// <returns></returns> private static char ToDelimiter(InputValidator.FileDelimiter value) { switch (value) { case InputValidator.FileDelimiter.CSV: return(','); case InputValidator.FileDelimiter.TSV: return('\t'); default: //we did not code against this delimiter type... throw out.. string errorMessage = string.Format("Unsupported file delimiter detected: {0}", value.ToString()); throw new InvalidEnumArgumentException(errorMessage); } }
/// <summary> /// Prompt the user for required information and process the input /// </summary> private static void AppStart() { //\\DT010G\Passport\Files //"c:\\jdev\\r\\Good.txt" Console.Clear(); PrintFancyMessage(UIMessages.MessageWelcome); //grab (and validate) the necessary info from the user string sourcFile = PromptUser <string>(validationHelper.IsValidFilePath, UIMessages.PromptFileLocation, UIMessages.PromptFileLocationHelp); InputValidator.FileDelimiter delimiter = PromptUser <InputValidator.FileDelimiter>(validationHelper.IsValidFileFormat, UIMessages.PromptFileDelimiter, UIMessages.PromptFileDelimiterHelp); int sourceFileFieldCount = PromptUser <int>(validationHelper.IsValidFieldCount, UIMessages.PromptFieldCount, UIMessages.PromptFieldCountHelp); //process the information.. ProcessFile(sourcFile, delimiter, sourceFileFieldCount); AppStall(true); }