private static void MyCopyAllFilesWithRoboCopy(Task buildTask, RobocopyWrapper myWrapper, string sourceFolder, string targetFolder, string wildCards, bool onlyReplaceWithNewerFile, bool continueOnError, string versionToCopy) { myWrapper.SourceFolder = sourceFolder; myWrapper.TargetFolder = targetFolder; myWrapper.FilesToInclude.Add(wildCards); myWrapper.UseRestartMode = false; myWrapper.CopyNewerFilesOnly = onlyReplaceWithNewerFile; if (Strings.Len(versionToCopy) > 0) { myWrapper.LogFileName = string.Format("FileDeploymentRobocopyLog_{0}.txt", versionToCopy); } else { myWrapper.LogFileName = "FileDeploymentRobocopyLog.txt"; } myWrapper.AppendToExistingLogFile = true; try { RoboCopyExitCodes codes = myWrapper.Execute(); switch (codes) { case RoboCopyExitCodes.SeveralFilesFailedtoCopy: case RoboCopyExitCodes.SevereErrorNoFilesCopied: if (!continueOnError) { throw new AutomationException(string.Format("RoboCopy returned an error code which indicates that some or all files may not have copied successfully: {0}", codes.ToString())); } MyLogWarning(buildTask, string.Format("RoboCopy returned an error code which indicates that some or all files may not have copied successfully: {0}", codes.ToString())); break; } } catch (Exception exception1) { ProjectData.SetProjectError(exception1); Exception exception = exception1; if (continueOnError) { MyLogWarning(buildTask, string.Format("Unexpected exception occurred while using RoboCopy. The continueOnError flag as set to true and the exception will be logged as a warning: {0}", exception.Message)); } ProjectData.ClearProjectError(); } }
public static void MyCopyAllFilesInFolder(Task buildTask, string sourceFolder, string targetFolder, string wildCards, bool overWrite, bool onlyReplaceWithNewerFile, bool continueOnError, Microsoft.VisualBasic.FileIO.SearchOption searchOption, bool useRoboCopy, string versionToCopy, string logText) { var myComputer = new Computer(); RobocopyWrapper myWrapper = null; if (useRoboCopy) { myWrapper = new RobocopyWrapper(); useRoboCopy = myWrapper.IsInstalled; if (useRoboCopy) { if (myWrapper.SupportsMultiThreading) { myWrapper.NumberOfThreads = 8; } else { myWrapper.NumberOfThreads = 1; } switch (searchOption) { case Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly: myWrapper.RecursiveMode = RoboCopyRecursiveCopy.CopyTopLevelOnly; break; case Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories: myWrapper.RecursiveMode = RoboCopyRecursiveCopy.CopyAllSubdirectories; break; } } } if (Strings.Len(wildCards) == 0) { wildCards = "*.*"; } if (Strings.Len(logText) == 0) { logText = "Copying folder from '{0}' to '{1}'..."; } if (Directory.Exists(sourceFolder)) { MyLogMessage(buildTask, string.Format(logText, sourceFolder, targetFolder), MessageImportance.Normal); if (useRoboCopy) { MyCopyAllFilesWithRoboCopy(buildTask, myWrapper, sourceFolder, targetFolder, wildCards, onlyReplaceWithNewerFile, continueOnError, versionToCopy); } else { //foreach (string str2 in _MyComputer.FileSystem.GetFiles() foreach (string str2 in myComputer.FileSystem.GetFiles(sourceFolder, searchOption, new string[] { wildCards })) { string targetFile = ""; try { string fullPath = Path.GetFullPath(sourceFolder); targetFile = PathHelper.Combine(targetFolder, Strings.Right(str2, (str2.Length - fullPath.Length) - 1)); MyCopyFile(buildTask, str2, targetFile, overWrite, onlyReplaceWithNewerFile, "Copying file from '{0}' to '{1}'...", false); } catch (Exception exception1) { ProjectData.SetProjectError(exception1); Exception ex = exception1; if (!continueOnError) { throw; } string exceptionData = GetExceptionData(ex); MyLogWarning(buildTask, string.Format("Error copying file '{0}' to '{1}'. The following error was raised: '{2}'.", str2, targetFile, exceptionData)); ProjectData.ClearProjectError(); } } } } else { MyLogMessage(buildTask, string.Format("Source folder '{0}' does not exist and cannot be copied to '{1}'.", sourceFolder, targetFolder), MessageImportance.Normal); } }