using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; namespace RoboSharp { /// /// /// /// /// /// public class JobOptions : ICloneable { // For more information, a good resource is here: https://adamtheautomator.com/robocopy/#Robocopy_Jobs #region < Constructors > /// /// /// public JobOptions() { } /// /// Constructor for ICloneable Interface /// /// JobOptions object to clone public JobOptions(JobOptions options) { FilePath = options.FilePath; NoDestinationDirectory = options.NoDestinationDirectory; NoSourceDirectory = options.NoSourceDirectory; PreventCopyOperation = options.PreventCopyOperation; } #endregion #region < ICloneable > /// /// Clone this JobOptions object /// /// New JobOptions object public JobOptions Clone() => new JobOptions(this); object ICloneable.Clone() => Clone(); #endregion #region < Constants > /// /// Take parameters from the named job file /// /// /// Usage: /JOB:"Path\To\File.RCJ" /// internal const string JOB_LOADNAME = " /JOB:"; /// /// Save parameters to the named job file /// /// /// Usage:
/// /SAVE:"Path\To\File" -> Creates Path\To\File.RCJ
/// /SAVE:"Path\To\File.txt" -> Creates Path\To\File.txt.RCJ
///
internal const string JOB_SAVE = " /SAVE:"; /// /// Quit after processing command line /// /// /// Used when writing JobFile /// internal const string JOB_QUIT = " /QUIT"; /// /// No source directory is specified /// internal const string JOB_NoSourceDirectory = " /NOSD"; /// /// No destination directory is specified /// internal const string JOB_NoDestinationDirectory = " /NODD"; #endregion #region < Properties > /// /// FilePath to save the Job Options (.RCJ) file to.
/// /SAVE:{FilePath} ///
/// /// This causes RoboCopy to generate an RCJ file where the command options are stored to so it can be used later.
/// and options are only evaluated if this is set.
///
public virtual string FilePath { get; set; } = ""; /// /// RoboCopy will validate the command, then exit before performing any Move/Copy/List operations.
/// /QUIT ///
/// /// This option is typically used when generating JobFiles. RoboCopy will exit after saving the Job FIle to the specified /// public virtual bool PreventCopyOperation { get; set; } /// /// path will not be saved to the JobFile.
/// /NOSD ///
/// /// Default value is False, meaning if is set, it will be saved to the JobFile RoboCopy generates. /// public virtual bool NoSourceDirectory { get; set; } /// /// path will not be saved to the JobFile.
/// /NODD ///
/// /// Default value is False, meaning if is set, it will be saved to the JobFile RoboCopy generates. /// public virtual bool NoDestinationDirectory { get; set; } #endregion #region < Methods > /// /// Parse the properties and return the string /// /// internal string Parse() { string options = ""; if (!FilePath.IsNullOrWhiteSpace()) { options += $"{JOB_SAVE}{FilePath.WrapPath()}"; if (NoSourceDirectory) options += JOB_NoSourceDirectory; if (NoDestinationDirectory) options += JOB_NoDestinationDirectory; } if (PreventCopyOperation) options += JOB_QUIT; return options; } /// /// Adds the 'NAME' and other properties into the JobFile /// internal void RunPostProcessing(RoboCommand cmd) { if (FilePath.IsNullOrWhiteSpace()) return; if (!File.Exists(FilePath)) return; var txt = File.ReadAllLines(FilePath).ToList(); //Write Options to JobFile txt.InsertRange(6, new string[] { "", "::", ":: Options for RoboSharp.JobFile", "::", JobFileBuilder.JOBFILE_JobName + " " + cmd.Name, JobFileBuilder.JOBFILE_StopIfDisposing + " " + cmd.StopIfDisposing.ToString().ToUpper() }); File.WriteAllLines(FilePath, txt); } /// /// Combine this object with another RetryOptions object.
/// not not be modified. ///
/// public void Merge(JobOptions options) { NoSourceDirectory |= options.NoSourceDirectory; NoDestinationDirectory |= options.NoDestinationDirectory; PreventCopyOperation |= options.PreventCopyOperation; } #endregion } }