private bool CopyFile(FolderMap baseFolder, string aFile) { // Just an input check if (aFile == null || aFile == "") { return(false); } if (_commandHandler.IgnoreFile(baseFolder, aFile)) { return(false); } string aTarget = aFile.Replace(baseFolder.Source, baseFolder.Target); bool doCopy = _commandHandler.NeedToCopy(baseFolder, aFile, aTarget); if (doCopy) { ProcessInfoEvent?.Invoke(this, new ProcessInfoEventArgs("Copy File: " + aFile + " to " + aTarget)); if (_commandHandler.CanReadFile(aFile)) { // Try if we can copy the file. _commandHandler.CopyFile(aFile, aTarget); } } return(doCopy); }
private bool CreateTargetDirectory(FolderMap baseFolder, string aFolder) { // OK copy files in this directory. // First check if target folder exists. string theTarget = aFolder.Replace(baseFolder.Source, baseFolder.Target); // if already exists we don't need to create it return(_commandHandler.CreateDirectory(theTarget)); }
public static Commands ParseArgs(string[] args) { bool showHelp = false; string source = ""; bool updatedOnly = false; bool requestConfirm = true; bool subFoldersAlso = false; bool skipCopyErrors = false; bool pauseWhenDone = false; string[] exclusiveExt = new string[1]; bool readCheckFirst = false; string target = string.Empty; EnumLogLevel logLevel = EnumLogLevel.None; // If not arguments supplied or help is requested set showHelp to true and stop processing. if (args == null || args.Length < 2 || args.Contains("/?")) { showHelp = true; } else { // Check other parms // 0 should be source Logger.Info("args(0)=" + args[0]); source = PathFormatter.FormatPath(args[0]); Logger.Debug("source=" + source); // 1 should be targed target = FormatTarget(args[1]); updatedOnly = args.Contains("/d", new CommandStringComparer()); requestConfirm = !args.Contains("/y", new CommandStringComparer()); subFoldersAlso = args.Contains("/s", new CommandStringComparer()); skipCopyErrors = args.Contains("/x", new CommandStringComparer()); pauseWhenDone = args.Contains("/p", new CommandStringComparer()); readCheckFirst = args.Contains("/r", new CommandStringComparer()); logLevel = GetLogLevel(args); Logger.Info("Loglevel = " + logLevel.ToString()); foreach (string aCmd in args) { if (aCmd.ToLower().StartsWith("/exclusiveext:")) { exclusiveExt = aCmd.Split(':')[0].Split('+'); break; } } } FolderMap folderMap = new FolderMap(source, target, updatedOnly, subFoldersAlso, exclusiveExt); List <FolderMap> folderMaps = new List <FolderMap>() { folderMap }; return(new Commands(showHelp, folderMaps, requestConfirm, skipCopyErrors, pauseWhenDone, readCheckFirst, logLevel)); }
public bool IgnoreFile(FolderMap baseMap, string file) { IFileIgnoreChecker fileIgnoreChecker; if (baseMap.ExclusiveExt.Length > 0) { fileIgnoreChecker = new IgnoreOnExtensionsChecker(baseMap.ExclusiveExt, _fileSystem, _exceptionHandler); } else { fileIgnoreChecker = new IgnoreNoneChecker(); } return(fileIgnoreChecker.IgnoreFile(baseMap, file)); }
public bool NeedToCopy(FolderMap baseMap, string aSource, string aTarget) { Logger.Debug($"baseMap.UpdatedOnly = {baseMap.UpdatedOnly}"); INeedToCopyChecker needToCopyChecker; if (baseMap.UpdatedOnly) { needToCopyChecker = new NeedToCopyUpdatedOnlyChecker(new SimpleFileComparer(), new FileSystem(), this); } else { needToCopyChecker = new NeedToCopyWithConfirmation(new FileSystem(), this); } return(needToCopyChecker.NeedToCopy(baseMap, aSource, aTarget)); }
private void ThisDirectory(FolderMap baseFolder, string aFolder) { if (!FolderExists(aFolder)) { return; } if (System.Text.RegularExpressions.Regex.IsMatch(aFolder, "(Temporary Internet Files)$")) { return; } ProcessInfoEvent?.Invoke(this, new ProcessInfoEventArgs("Process directory: " + aFolder)); if (!CreateTargetDirectory(baseFolder, aFolder)) { return; } string[] theFiles; int nrOfCopiedFiles = 0; // Now copy files try { theFiles = _fileSystem.Directory.GetFiles(aFolder); foreach (string aFile in theFiles) { if (CopyFile(baseFolder, aFile)) { nrOfCopiedFiles += 1; } } Logger.Info($"Copied {nrOfCopiedFiles} of {theFiles.Length} in folder {aFolder}"); } catch (UnauthorizedAccessException ex) { _commandHandler.HandleException("Could not read directories of folder: " + aFolder + "(" + ex.Message + ")", ex); } ProcessSubfolders(baseFolder, aFolder); }
private void ProcessSubfolders(FolderMap baseFolder, string aFolder) { // When we want the subfolders also we need to loop thru the sub directories. if (baseFolder.SubFoldersAlso) { string[] directories = new string[1]; try { directories = _fileSystem.Directory.GetDirectories(aFolder); } catch (UnauthorizedAccessException ex) { _commandHandler.HandleException($"Failed to read directories within directory {aFolder}", ex); } foreach (var aDirectory in directories) { ThisDirectory(baseFolder, aDirectory); } } }