/// <summary> /// Handles the deletion of the passed target /// </summary> /// <param name="target">The file or directory to be deleted</param> /// <param name="mt">Multithreading configuration</param> /// <param name="log">Logging configuration</param> /// <param name="output">Output enabled</param> /// <returns>An error code based on how the operation turned out</returns> public static int Delete(Target target, MultithreadingSetup mt, Logging log, bool output) { int retval = (int) ErrorCodes.Success; if (target.Exists) { // Create empty directory for mirroring string emptyDir = SystemTempDir + @"\FilExile_temp$"; string secondEmptyDir = ""; Directory.CreateDirectory(emptyDir); // Sometimes there's an extra backslash on the end of the path // and we need to trim it off if (target.Path.EndsWith(@"\")) target.Path = target.Path.TrimEnd('\\'); if (target.IsDirectory) { _robocopyCommand = PrepareRobocopyCommand(mt, log, emptyDir, target.Path); } else { // For single files there is an extra step we have to take... // We need to create another temporary directory. We are going to use Robocopy // to place the file in this temporary directory by itself. This is to // prevent the actual diretory mirror command from wiping out everything else // where the file was found. secondEmptyDir = SystemTempDir + @"\FilExile_singleFile_temp$"; Directory.CreateDirectory(secondEmptyDir); string fileCopyCmd = PrepareFileCopyCommand(target.ParentDirectory, secondEmptyDir, target.FileName); RunRobocopy(fileCopyCmd, output); _robocopyCommand = PrepareRobocopyCommand(mt, log, emptyDir, secondEmptyDir); } // This is where the main deletion operation occurs - Uses Robocopy to mirror // the empty directory we created onto the target. This will make any files within // the target appear as "extra files" and forcibly remove them via Robocopy which // can handle all sorts of nasty files that Windows will sometimes choke on RunRobocopy(_robocopyCommand, output); // Delete the temporary directory/directories created if (Directory.Exists(emptyDir)) { Directory.Delete(emptyDir); } if (Directory.Exists(secondEmptyDir)) { Directory.Delete(secondEmptyDir); } } else { retval = (int) ErrorCodes.NotFound; } return retval; }
/// <summary> /// Takes the two passed directories and creates Robocopy arguments to mirror the /// empty directory onto the target diretory /// </summary> /// <param name="mt">Multithreading struct (enabled, numThreads)</param> /// <param name="log">Logging struct (enabled, logfile)</param> /// <param name="emptyDir">Empty directory to mirror from</param> /// <param name="targetDir">Targeted directory to be eliminated</param> /// <returns>Prepared Robocopy arguments to mirror the emptyDir onto the targetDir</returns> private static string PrepareRobocopyCommand(MultithreadingSetup mt, Logging log, string emptyDir, string targetDir) { string retVal = string.Empty; retVal += "\"" + emptyDir + "\" \"" + targetDir + "\" /MIR /NJH /NP"; if (mt.ThreadingEnabled) { if (mt.NumThreads < 1 || mt.NumThreads > 128) mt.NumThreads = 8; retVal += " /MT:" + mt.NumThreads; } if (!string.IsNullOrEmpty(log.LogTo) && log.Enabled) { retVal += @" /TEE /V /LOG+:" + log.LogTo + "\""; } return retVal; }