protected bool CleanupFilePaths(ref string strInputFilePath, ref string strOutputFolderPath) { // Returns True if success, False if failure bool blnSuccess; try { // Make sure strInputFilePath points to a valid file var fiInputFile = new FileInfo(strInputFilePath); if (!fiInputFile.Exists) { if (ShowMessages) { ShowErrorMessage("Input file not found: " + strInputFilePath); } else { LogMessage("Input file not found: " + strInputFilePath, eMessageTypeConstants.ErrorMsg); } mErrorCode = eProcessFilesErrorCodes.InvalidInputFilePath; blnSuccess = false; } else { if (string.IsNullOrWhiteSpace(strOutputFolderPath)) { // Define strOutputFolderPath based on strInputFilePath strOutputFolderPath = fiInputFile.DirectoryName; if (string.IsNullOrEmpty(strOutputFolderPath)) { strOutputFolderPath = "."; } } // Make sure strOutputFolderPath points to a folder var diOutputFolder = new DirectoryInfo(strOutputFolderPath); if (!diOutputFolder.Exists) { // strOutputFolderPath points to a non-existent folder; attempt to create it diOutputFolder.Create(); } mOutputFolderPath = string.Copy(diOutputFolder.FullName); blnSuccess = true; } } catch (Exception ex) { HandleException("Error cleaning up the file paths", ex); return(false); } return(blnSuccess); }
protected bool CleanupInputFilePath(ref string strInputFilePath) { // Returns True if success, False if failure bool blnSuccess; try { // Make sure strInputFilePath points to a valid file var fiInputFile = new FileInfo(strInputFilePath); if (!fiInputFile.Exists) { if (ShowMessages) { ShowErrorMessage("Input file not found: " + strInputFilePath); } else { LogMessage("Input file not found: " + strInputFilePath, eMessageTypeConstants.ErrorMsg); } mErrorCode = eProcessFilesErrorCodes.InvalidInputFilePath; blnSuccess = false; } else { blnSuccess = true; } } catch (Exception ex) { HandleException("Error cleaning up the file paths", ex); return(false); } return(blnSuccess); }
protected void SetBaseClassErrorCode(eProcessFilesErrorCodes eNewErrorCode) { mErrorCode = eNewErrorCode; }
private bool RecurseFoldersWork(string strInputFolderPath, string strFileNameMatch, string strOutputFolderName, string strParameterFilePath, string strOutputFolderAlternatePath, bool blnRecreateFolderHierarchyInAlternatePath, string[] strExtensionsToParse, ref int intFileProcessCount, ref int intFileProcessFailCount, int intRecursionLevel, int intRecurseFoldersMaxLevels) { // If intRecurseFoldersMaxLevels is <=0 then we recurse infinitely DirectoryInfo diInputFolderInfo; int intExtensionIndex; bool blnProcessAllExtensions = false; string strOutputFolderPathToUse; bool blnSuccess; try { diInputFolderInfo = new DirectoryInfo(strInputFolderPath); } catch (Exception ex) { // Input folder path error HandleException("Error in RecurseFoldersWork", ex); mErrorCode = eProcessFilesErrorCodes.InvalidInputFilePath; return(false); } try { if (!string.IsNullOrWhiteSpace(strOutputFolderAlternatePath)) { if (blnRecreateFolderHierarchyInAlternatePath) { strOutputFolderAlternatePath = Path.Combine(strOutputFolderAlternatePath, diInputFolderInfo.Name); } strOutputFolderPathToUse = Path.Combine(strOutputFolderAlternatePath, strOutputFolderName); } else { strOutputFolderPathToUse = strOutputFolderName; } } catch (Exception ex) { // Output file path error HandleException("Error in RecurseFoldersWork", ex); mErrorCode = eProcessFilesErrorCodes.InvalidOutputFolderPath; return(false); } try { // Validate strExtensionsToParse[) for (intExtensionIndex = 0; intExtensionIndex <= strExtensionsToParse.Length - 1; intExtensionIndex++) { if (strExtensionsToParse[intExtensionIndex] == null) { strExtensionsToParse[intExtensionIndex] = string.Empty; } else { if (!strExtensionsToParse[intExtensionIndex].StartsWith(".")) { strExtensionsToParse[intExtensionIndex] = "." + strExtensionsToParse[intExtensionIndex]; } if (strExtensionsToParse[intExtensionIndex] == ".*") { blnProcessAllExtensions = true; break; } strExtensionsToParse[intExtensionIndex] = strExtensionsToParse[intExtensionIndex].ToUpper(); } } } catch (Exception ex) { HandleException("Error in RecurseFoldersWork", ex); mErrorCode = eProcessFilesErrorCodes.UnspecifiedError; return(false); } try { if (!string.IsNullOrWhiteSpace(strOutputFolderPathToUse)) { // Update the cached output folder path mOutputFolderPath = string.Copy(strOutputFolderPathToUse); } ShowMessage("Examining " + strInputFolderPath); // Process any matching files in this folder blnSuccess = true; foreach (FileInfo ioFileMatch in diInputFolderInfo.GetFiles(strFileNameMatch)) { for (intExtensionIndex = 0; intExtensionIndex <= strExtensionsToParse.Length - 1; intExtensionIndex++) { if (blnProcessAllExtensions || ioFileMatch.Extension.ToUpper() == strExtensionsToParse[intExtensionIndex]) { blnSuccess = ProcessFile(ioFileMatch.FullName, strOutputFolderPathToUse, strParameterFilePath, true); if (!blnSuccess) { intFileProcessFailCount += 1; blnSuccess = true; } else { intFileProcessCount += 1; } break; } if (mAbortProcessing) { break; } } } } catch (Exception ex) { HandleException("Error in RecurseFoldersWork", ex); mErrorCode = eProcessFilesErrorCodes.InvalidInputFilePath; return(false); } if (!mAbortProcessing) { // If intRecurseFoldersMaxLevels is <=0 then we recurse infinitely // otherwise, compare intRecursionLevel to intRecurseFoldersMaxLevels if (intRecurseFoldersMaxLevels <= 0 || intRecursionLevel <= intRecurseFoldersMaxLevels) { // Call this function for each of the subfolders of diInputFolderInfo foreach (DirectoryInfo ioSubFolderInfo in diInputFolderInfo.GetDirectories()) { blnSuccess = RecurseFoldersWork(ioSubFolderInfo.FullName, strFileNameMatch, strOutputFolderName, strParameterFilePath, strOutputFolderAlternatePath, blnRecreateFolderHierarchyInAlternatePath, strExtensionsToParse, ref intFileProcessCount, ref intFileProcessFailCount, intRecursionLevel + 1, intRecurseFoldersMaxLevels); if (!blnSuccess) { break; } } } } return(blnSuccess); }
// Main function for processing files in a folder (and subfolders) public bool ProcessFilesAndRecurseFolders(string strInputFilePathOrFolder, string strOutputFolderName, string strOutputFolderAlternatePath, bool blnRecreateFolderHierarchyInAlternatePath, string strParameterFilePath, int intRecurseFoldersMaxLevels, string[] strExtensionsToParse) { // Calls ProcessFiles for all files in strInputFilePathOrFolder and below having an extension listed in strExtensionsToParse[) // The extensions should be of the form ".TXT" or ".RAW" (i.e. a period then the extension) // If any of the extensions is "*" or ".*" then all files will be processed // If strInputFilePathOrFolder contains a filename with a wildcard (* or ?), then that information will be // used to filter the files that are processed // If intRecurseFoldersMaxLevels is <=0 then we recurse infinitely bool blnSuccess; // Examine strInputFilePathOrFolder to see if it contains a filename; if not, assume it points to a folder // First, see if it contains a * or ? try { string strInputFolderPath; if ((strInputFilePathOrFolder != null) && (strInputFilePathOrFolder.Contains("*") || strInputFilePathOrFolder.Contains("?"))) { // Copy the path into strCleanPath and replace any * or ? characters with _ string strCleanPath = strInputFilePathOrFolder.Replace("*", "_"); strCleanPath = strCleanPath.Replace("?", "_"); var fiInputFileSpec = new FileInfo(strCleanPath); if (fiInputFileSpec.Directory != null && fiInputFileSpec.Directory.Exists) { strInputFolderPath = fiInputFileSpec.DirectoryName; } else { // Use the directory that has the .exe file strInputFolderPath = GetAppFolderPath(); } // Remove any directory information from strInputFilePath strInputFilePathOrFolder = Path.GetFileName(strInputFilePathOrFolder); } else { if (string.IsNullOrWhiteSpace(strInputFilePathOrFolder)) { mErrorCode = eProcessFilesErrorCodes.InvalidInputFilePath; ShowErrorMessage("Input file path is empty in ProcessFilesAndRecurseFolders"); return(false); } var diInputFolderSpec = new DirectoryInfo(strInputFilePathOrFolder); if (diInputFolderSpec.Exists) { strInputFolderPath = diInputFolderSpec.FullName; strInputFilePathOrFolder = "*"; } else { if (diInputFolderSpec.Parent != null && diInputFolderSpec.Parent.Exists) { strInputFolderPath = diInputFolderSpec.Parent.FullName; strInputFilePathOrFolder = Path.GetFileName(strInputFilePathOrFolder); } else { // Unable to determine the input folder path strInputFolderPath = string.Empty; } } } if (!string.IsNullOrWhiteSpace(strInputFolderPath)) { // Validate the output folder path if (!string.IsNullOrWhiteSpace(strOutputFolderAlternatePath)) { try { var diOutputFolder = new DirectoryInfo(strOutputFolderAlternatePath); if (!diOutputFolder.Exists) { diOutputFolder.Create(); } } catch (Exception ex) { mErrorCode = eProcessFilesErrorCodes.InvalidOutputFolderPath; ShowErrorMessage("Error validating the alternate output folder path in ProcessFilesAndRecurseFolders:" + ex.Message); return(false); } } // Initialize some parameters mAbortProcessing = false; int intFileProcessCount = 0; int intFileProcessFailCount = 0; // Call RecurseFoldersWork const int intRecursionLevel = 1; blnSuccess = RecurseFoldersWork(strInputFolderPath, strInputFilePathOrFolder, strOutputFolderName, strParameterFilePath, strOutputFolderAlternatePath, blnRecreateFolderHierarchyInAlternatePath, strExtensionsToParse, ref intFileProcessCount, ref intFileProcessFailCount, intRecursionLevel, intRecurseFoldersMaxLevels); } else { mErrorCode = eProcessFilesErrorCodes.InvalidInputFilePath; return(false); } } catch (Exception ex) { HandleException("Error in ProcessFilesAndRecurseFolders", ex); return(false); } return(blnSuccess); }
public bool ProcessFilesWildcard(string strInputFilePath, string strOutputFolderPath, string strParameterFilePath, bool blnResetErrorCode) { // Returns True if success, False if failure bool blnSuccess; mAbortProcessing = false; blnSuccess = true; try { // Possibly reset the error code if (blnResetErrorCode) { mErrorCode = eProcessFilesErrorCodes.NoError; } if (!string.IsNullOrWhiteSpace(strOutputFolderPath)) { // Update the cached output folder path mOutputFolderPath = string.Copy(strOutputFolderPath); } // See if strInputFilePath contains a wildcard (* or ?) if ((strInputFilePath != null) && (strInputFilePath.Contains("*") || strInputFilePath.Contains("?"))) { // Obtain a list of the matching files // Copy the path into strCleanPath and replace any * or ? characters with _ string strCleanPath = strInputFilePath.Replace("*", "_"); strCleanPath = strCleanPath.Replace("?", "_"); var fiInputFileSpec = new FileInfo(strCleanPath); string strInputFolderPath; if (fiInputFileSpec.Directory != null && fiInputFileSpec.Directory.Exists) { strInputFolderPath = fiInputFileSpec.DirectoryName; } else { // Use the directory that has the .exe file strInputFolderPath = GetAppFolderPath(); } if (string.IsNullOrEmpty(strInputFolderPath)) { strInputFolderPath = "."; } var diInputFolder = new DirectoryInfo(strInputFolderPath); // Remove any directory information from strInputFilePath strInputFilePath = Path.GetFileName(strInputFilePath); int intMatchCount = 0; foreach (var fiInputFile in diInputFolder.GetFiles(strInputFilePath)) { intMatchCount += 1; blnSuccess = ProcessFile(fiInputFile.FullName, strOutputFolderPath, strParameterFilePath, blnResetErrorCode); if (mAbortProcessing) { break; } if (!blnSuccess && !mIgnoreErrorsWhenUsingWildcardMatching) { break; } if (intMatchCount % 100 == 0) { Console.Write("."); } } if (intMatchCount == 0) { if (mErrorCode == eProcessFilesErrorCodes.NoError) { if (ShowMessages) { ShowErrorMessage("No match was found for the input file path: " + strInputFilePath); } else { LogMessage("No match was found for the input file path: " + strInputFilePath, eMessageTypeConstants.ErrorMsg); } } } else { Console.WriteLine(); } } else { blnSuccess = ProcessFile(strInputFilePath, strOutputFolderPath, strParameterFilePath, blnResetErrorCode); } } catch (Exception ex) { HandleException("Error in ProcessFilesWildcard", ex); return(false); } return(blnSuccess); }