public void Move(string path, bool execute) { Path = path; BatchRunner BR = new BatchRunner(path + "\\JunctionQuery.bat", path + "\\Junctions.txt", CodePage); foreach (string dir in Directories) { BR.AddCommand("dir \"" + dir + "\" /AL /S"); } BR.TaskCompleted += JunctionQueryCompleted; Execute = execute; BR.Run(); }
void JunctionQueryCompleted(object sender, BatchRunner.TaskCompletedEventArgs e) { string dateTail = DateTime.Now.ToString("_yyyy-MM-dd HH-mm-ss"); List <Junction> junctions = new List <Junction>(); Regex junctionRegex = new Regex(@"<JUNCTION>\s*([^[]*)\[([^]]*)]"); string dir = null; foreach (string line in e.Output) { if (line.StartsWith(" Directory of ")) { dir = line.Substring(14); } else if (line.Contains("<JUNCTION>")) { Match M = junctionRegex.Match(line); junctions.Add(new Junction(dir + "\\" + M.Groups[1].Value.TrimEnd(), M.Groups[2].Value)); } } foreach (Junction J in junctions) { try { DirectoryInfo DI = new DirectoryInfo(J.Source); J.Hidden = DI.Attributes.HasFlag(FileAttributes.Hidden); } catch { } } BatchRunner mover = new BatchRunner(Path + "\\MoveJunctions" + dateTail + ".bat", Path + "\\MoveLog.txt", CodePage); BatchRunner recovery = new BatchRunner(Path + "\\RecoverJunctions" + dateTail + ".bat", null, CodePage); BatchRunner finalizer = new BatchRunner(Path + "\\FinalizeJunctions" + dateTail + ".bat", null, CodePage); mover.Echo("Moving directories..."); string[] destDirs = new string[Directories.Count]; int i = 0; foreach (string d in Directories) { destDirs[i] = DestinationPath + d.Substring(3); mover.AddCommand("robocopy \"" + d + "\" \"" + destDirs[i] + "\" /MIR /XJ /COPYALL /ZB"); i++; } mover.Echo("Renaming directories..."); bool hidden, system; foreach (string d in Directories) { hidden = false; system = false; try { DirectoryInfo DI = new DirectoryInfo(d); hidden = DI.Attributes.HasFlag(FileAttributes.Hidden); system = DI.Attributes.HasFlag(FileAttributes.System); } catch { } if (system || hidden) { mover.AddCommand("attrib " + (system ? "-S " : "") + (hidden ? "-H " : "") + "\"" + d + "\"", true); recovery.AddCommand("attrib " + (system ? "-S " : "") + (hidden ? "-H " : "") + "\"" + d + "\""); } mover.AddCommand("rename \"" + d + "\" \"" + d.Substring(d.LastIndexOf('\\') + 1) + RenameTail + "\"", true); mover.AddCommand("attrib +H \"" + d + RenameTail + "\"", true); recovery.AddCommand("attrib -H \"" + d + RenameTail + "\""); recovery.AddCommand("rmdir \"" + d + "\" /Q"); recovery.AddCommand("rename \"" + d + RenameTail + "\" \"" + d.Substring(d.LastIndexOf('\\') + 1) + "\"", true); if (hidden || system) { recovery.AddCommand("attrib " + (system ? "+S " : "") + (hidden ? "+H " : "") + "\"" + d + "\""); } finalizer.AddCommand("attrib -H \"" + SourceVolumeOnTargetOS + d.Substring(1) + RenameTail + "\""); finalizer.AddCommand("rmdir \"" + SourceVolumeOnTargetOS + d.Substring(1) + RenameTail + "\" /S /Q"); } recovery.FinalizeFile(); finalizer.FinalizeFile(); string destinationPathForTargetOS = DestinationVolumeOnTargetOS + DestinationPath.Substring(1); mover.Echo("Making softlinks..."); foreach (string d in Directories) { mover.AddCommand("mklink /J \"" + d + "\" \"" + destinationPathForTargetOS + d.Substring(3) + "\"", true); hidden = false; system = false; try { DirectoryInfo DI = new DirectoryInfo(d); hidden = DI.Attributes.HasFlag(FileAttributes.Hidden); system = DI.Attributes.HasFlag(FileAttributes.System); if (system || hidden) { mover.AddCommand("attrib " + (system ? "+S " : "") + (hidden ? "+H " : "") + "\"" + d + "\" /L", true); } } catch { } } mover.Echo("Making junctions..."); foreach (Junction j in junctions) { string source = DestinationPath + j.Source.Substring(3); string target = j.Target; mover.AddCommand("mklink /J \"" + source + "\" \"" + target + "\"", true); if (j.Hidden) { mover.AddCommand("attrib +H \"" + source + "\"", true); } } mover.TaskCompleted += mover_TaskCompleted; if (Execute) { mover.Run(); } else { mover.FinalizeFile(); } }