/// <summary> /// Undo the operation executed by a dirMv() method. /// </summary> /// <param name="usedSource"></param> /// <param name="usedDestination"></param> /// <param name="showUi"></param> public static bool undoMv(string usedSource, string usedDestination, bool showUi, out string errSrc, out string errDst, out string errMsg, out bool opWasCancelled) { // move src dst : move "TestDir\\t2" "TestDir\\t1" // before "TestDir\\t2" "TestDir\\t1" => after: "TestDir\\t1" "TestDir\\t1\\t2" // undomove: move source "TestDir\\t1\\t2" ; move destination "TestDir\\" // new source char[] charSeparators = new char[] { '\\' }; string[] dirsSrc = usedSource.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries); string lastElementSrc = dirsSrc[dirsSrc.Length - 1]; string newSource = usedDestination + "\\" + lastElementSrc; // new dst string[] dirsDst = usedSource.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries); string newDst = ""; for (int i = 0; i < dirsDst.Length - 1; i++) { if (i == 0) { newDst = dirsDst[0]; } else { newDst += "\\" + dirsDst[i]; } } newDst += "\\"; try { CSharp.dirMv(newSource, newDst, showUi); } catch (OperationCanceledException oce) { errSrc = newSource; errDst = newDst; errMsg = oce.Message; opWasCancelled = true; return(false); } catch (Exception ex) { errSrc = newSource; errDst = newDst; errMsg = ex.Message; opWasCancelled = false; return(false); } errSrc = ""; errDst = ""; errMsg = ""; opWasCancelled = false; return(true); }
/// <summary> /// Execute a sequence of dirMv operations. if any fail, it will undo all operations executed. /// This command will fail on the follow situations: /// (1) If the lenght of the source or destination array do not match /// (2) If any of the sources are repeated /// (3) If any of the sources matches with any of the destinations /// /// </summary> /// <param name="sources"></param> /// <param name="destinations"></param> /// <param name="showUi"></param> /// <param name="errMsg"></param> /// <returns></returns> public static bool stackMv(string[] sources, string[] destinations, bool showUi, LogMethod logMethod, out string errMsg, out string errSrcDir, out string errDstDir, out bool opWasCancelled) { bool opWasCancelledTemp = false; bool exceptionError = false; bool retVal = true; opWasCancelled = false; CSharp.stlogMethod = logMethod; if (sources == null || destinations == null) { errMsg = "NULL VECTOR ERROR"; errSrcDir = ""; errDstDir = ""; return(false); } if (sources.Length != destinations.Length) { errMsg = "SOURCE and DESTINATION stack sizes do not match! src:" + sources.Length + ", dst:" + destinations.Length; errSrcDir = ""; errDstDir = ""; return(false); } // before moving, check if the src and dst exist and dont match for (int i = 0; i < sources.Length; i++) { string currentItem = sources[i]; //-- check if there is any repeated element on the src list of elements for (int j = 0; j < sources.Length; j++) { if (i == j) { continue; } else { // if two elements are equal if ((currentItem.Trim() == sources[j].Trim())) { errMsg = "Error: item[" + i + "] == item[" + j + "] " + currentItem + "::" + sources[j]; errDstDir = ""; errSrcDir = currentItem; log.Warn(errMsg); return(false); } } } //-- check if there is any element on the sources list exit on the destination list for (int j = 0; j < destinations.Length; j++) { if (currentItem.Trim() == destinations[j].Trim()) { errMsg = "Error: item[" + i + "] == item[" + j + "] " + currentItem + "::" + sources[j]; errDstDir = ""; errSrcDir = currentItem; log.Warn(errMsg); return(false); } } } //-- check if all src and dst dirs do exit foreach (var item in sources) { if (!Directory.Exists(item)) { errMsg = "Source directory does not exit {" + item + "}"; errDstDir = ""; errSrcDir = item; log.Warn("ERROR: " + errMsg); return(false); } } foreach (var item in destinations) { if (!Directory.Exists(item)) { errMsg = "Destination directory does not exit {" + item + "}"; errDstDir = item; errSrcDir = ""; log.Warn("ERROR: " + errMsg); return(false); } } // Move the directories Stack <string> dirStackSrc = new Stack <string>(); Stack <string> dirStackDst = new Stack <string>(); errMsg = ""; for (int i = 0; i < sources.Length; i++) { try { if (!sources[i].Trim().Equals("") && !destinations[i].Trim().Equals("")) { CSharp.dirMv(sources[i], destinations[i], showUi); dirStackSrc.Push(sources[i]); dirStackDst.Push(destinations[i]); } } catch (OperationCanceledException oce) { //retVal = true; errMsg = oce.Message; opWasCancelled = true; exceptionError = true; } catch (Exception ex) { retVal = false; errMsg = ex.Message; opWasCancelled = false; exceptionError = true; } if (exceptionError) { while (dirStackSrc.Count > 0) { bool retUndo = undoMv(dirStackSrc.Pop(), dirStackDst.Pop(), showUi, out errSrcDir, out errDstDir, out errMsg, out opWasCancelledTemp); } errSrcDir = sources[i]; errDstDir = destinations[i]; // if OperationCanceledException or sucess return true // other exception return false return(false); } } errSrcDir = ""; errDstDir = ""; errMsg = "SUCCESS!"; return(true); }