static void PadFiles(RunArgs ra, List <FileInfo> FI, bool forreal = false) { var tcounter = ra.State.NameCounter; foreach (var f in ra.State.Files) { if (f.FullName == ra.Me) { continue; } var dotidx = f.Name.IndexOf('.'); var fn = f.Name.Remove(dotidx); var suff = f.Name.Substring(dotidx); fn = PadZeroes(fn, ra.State.PadZeroes); var newname = string.Format("{0}{1}{2}", ra.State.Prefix, fn, suff); var newpath = Path.Combine(ra.State.DestinationDir, newname); if (!forreal) { string.Format(f.Name + " --> " + newpath).PrintLine(ConsoleColor.Yellow); } else { File.Move(f.FullName, newpath); FI.Add(new FileInfo(newpath)); } } }
void search(RunArgs ra, DirectoryInfo d) { var All = new List <List <FileInfo> >(); var searchOpt = SearchOption.AllDirectories; try { foreach (var sd in d.EnumerateDirectories("*", searchOpt).Union(new DirectoryInfo[] { d })) { try { var L = new List <FileInfo>(); foreach (var file in sd.EnumerateFiles(ra.State.SearchPattern, SearchOption.TopDirectoryOnly)) { L.Add(file); } All.Add(L); } catch (Exception ex) { ex.Message.PrintLine(ConsoleColor.Red); } } } catch (Exception ex) { ex.Message.PrintLine(ConsoleColor.Red); } }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var tf = string.Empty; var allText = string.Empty; var quiet = false; if (interactive) { Utils.ReadString("File: ", ref tf, true); allText = File.ReadAllText(tf); var q = string.Empty; Utils.ReadWord("Quiet mode (y/*): ", q); if (q == "y") { quiet = true; } } else { if (ra.InArgs.ContainsKey("-tf")) { allText = File.ReadAllText(ra.InArgs.GetFirstValue("-tf")); } else { throw new ArgumentNullException("-tf"); } if (ra.InArgs.ContainsKey("-q")) { quiet = true; } } var victims = allText.Split(Environment.NewLine); var counter = 0; foreach (var line in victims) { if (line.Length > 0) { if (File.Exists(line)) { File.Delete(line); counter++; } else if (!quiet) { $"File not found: {line}".PrintLine(); } } } $"Done, {counter} files were deleted.".PrintLine(); return(0); }
static void PadZeroes(ref string filename, RunArgs ra) { Match m = Regex.Match(filename, @"\d+"); if (m.Success) { var padded = m.Value.PadLeft(ra.State.PadZeroes, '0'); filename = filename.Replace(m.Value, padded); } }
public int Run(RunArgs ra) { ra.State.DestinationDir = ra.RootDir.FullName; Utils.ReadString("destination dir: ", ref ra.State.DestinationDir); Utils.ReadInt("number length with zero padding (6) : ", ref ra.State.PadZeroes); Utils.ReadString("search pattern (*.*): ", ref ra.State.SearchPattern); var useexisting = false; if (ra.State.Files != null && ra.State.Files.Length > 0) { string.Format( "There are {0} files from a previous subprogram run. {1}Frst of the old files: {2} ", ra.State.Files.Length, Environment.NewLine, ra.State.Files[0]).PrintLine(); useexisting = Utils.ReadWord("Use them? (y/*) ", "y"); } if (!useexisting) { ra.State.Files = ra.RootDir.GetFiles(ra.State.SearchPattern, SearchOption.TopDirectoryOnly); } else if (Utils.ReadWord(string.Format("Change the root dir ({0}) ? (y/*)", ra.RootDir.FullName), "y")) { var newroot = string.Empty; Utils.ReadString("Enter new root dir: ", ref newroot, true); ra.ChangeRoot(newroot); } string.Format("There are {0} matches.", ra.State.Files.Length).PrintLine(); ra.Trace = Utils.ReadWord("Trace first? (y/*): ", "y"); var FI = new List <FileInfo>(); if (ra.Trace) { PadFiles(ra, FI); } if (Utils.ReadWord("Pad all? (y/*): ", "y")) { PadFiles(ra, FI, true); ra.State.Files = FI.ToArray(); $"Done - {ra.State.Files.Length} files renamed.".PrintLine(); } else { "Aborting pad.".PrintLine(); } return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var allText = string.Empty; var tf = string.Empty; var desc = false; if (interactive) { Utils.ReadString("File: ", ref tf, true); desc = Utils.ReadWord("Sort in descending order? [by default is asc] (y/*): ", "y"); } else { if (ra.InArgs.ContainsKey("-tf")) { tf = ra.InArgs.GetFirstValue("-tf"); } else { throw new ArgumentNullException("-tf"); } if (ra.InArgs.ContainsKey("-desc")) { desc = true; } } allText = File.ReadAllText(tf); var allLines = allText.Split(Environment.NewLine); var len = allLines.Length; Array.Sort(allLines); if (desc) { Array.Reverse(allLines); } File.Delete(tf); File.WriteAllLines(tf, allLines); "Done.".PrintLine(); return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var allText = string.Empty; var tf = string.Empty; if (interactive) { Utils.ReadString("File: ", ref tf, true); } else { if (ra.InArgs.ContainsKey("-tf")) { tf = ra.InArgs.GetFirstValue("-tf"); } else { throw new ArgumentNullException("-tf"); } } allText = File.ReadAllText(tf); var allLines = allText.Split(Environment.NewLine); var rdm = new Random(); var len = allLines.Length; for (int i = 0; i < len; i++) { var idx = rdm.Next(0, len); var tmp = allLines[i]; allLines[i] = allLines[idx]; allLines[idx] = tmp; } File.Delete(tf); File.WriteAllLines(tf, allLines); "Done.".PrintLine(); return(0); }
public int Run(RunArgs ra) { interactive = !ra.InArgs.ContainsKey("-ni"); if (interactive) { Utils.ReadString("Images search pattern (*.jpg by default): ", ref sp, false); Utils.ReadString("Images map file: ", ref fmap, false); if (!string.IsNullOrEmpty(fmap)) { paths = File.ReadAllText(fmap).Split(Environment.NewLine); } else { "No map file was provided, will use the current dir as a source.".PrintLine(); paths = Directory.GetFiles(ra.RootDir.FullName, sp); } if (paths != null && paths.Length > 0) { Utils.ReadString("Destination folder: ", ref dest, true); Utils.ReadString("Mode [h, v, hv or r90]: ", ref mode, true); if (Array.IndexOf(MODES, mode) < 0) { throw new ArgumentNullException("-mode", "Incorrect mode"); } Utils.ReadString("Result image filename prefix [optional]: ", ref prf, false); Utils.ReadString("Result image filename suffix [optional]: ", ref sfx, false); } else { throw new ArgumentException("", $"There are no images in {ra.RootDir.FullName}"); } } else { if (ra.InArgs.ContainsKey("-out")) { dest = ra.InArgs.GetFirstValue("-out"); } else { throw new ArgumentNullException("-out"); } if (ra.InArgs.ContainsKey("-mode")) { mode = ra.InArgs.GetFirstValue("-mode"); if (Array.IndexOf(MODES, mode) < 0) { throw new ArgumentException("-mode", "Incorrect mode."); } } else { throw new ArgumentNullException("-mode"); } if (ra.InArgs.ContainsKey("-sp")) { sp = ra.InArgs.GetFirstValue("-sp"); } if (ra.InArgs.ContainsKey("-map")) { paths = File.ReadAllText(ra.InArgs.GetFirstValue("-map")).Split(Environment.NewLine); } else { "No map file was provided, will use the current dir as a source.".PrintLine(); paths = Directory.GetFiles(ra.RootDir.FullName, sp); } if (paths == null || paths.Length < 1) { throw new ArgumentException("", $"There are no images in {ra.RootDir.FullName}"); } if (ra.InArgs.ContainsKey("-prf")) { prf = ra.InArgs.GetFirstValue("-prf"); } if (ra.InArgs.ContainsKey("-sfx")) { prf = ra.InArgs.GetFirstValue("-sfx"); } } if (string.IsNullOrEmpty(prf)) { prf = string.Empty; } if (string.IsNullOrEmpty(sfx)) { sfx = string.Empty; } if (!Directory.Exists(dest)) { Directory.CreateDirectory(dest); } flip(); return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var baseUrl = string.Empty; var linksFile = string.Empty; var reqPerSec = 5.0; var fromIdx = 0; var toIdx = -1; var url = string.Empty; ra.State.DestinationDir = ra.RootDir.FullName; if (interactive) { Utils.ReadString("destination dir: ", ref ra.State.DestinationDir); Utils.ReadString("url: ", ref url); if (string.IsNullOrWhiteSpace(url)) { Utils.ReadString("base url: ", ref baseUrl); Utils.ReadString("links file: ", ref linksFile, true); Utils.ReadDouble("req/sec: ", ref reqPerSec); Utils.ReadInt("from row: ", ref fromIdx); Utils.ReadInt("to row: ", ref toIdx); } } else { if (ra.InArgs.ContainsKey("-f")) { linksFile = ra.InArgs.GetFirstValue("-f"); } if (ra.InArgs.ContainsKey("-base")) { baseUrl = ra.InArgs.GetFirstValue("-base"); } if (ra.InArgs.ContainsKey("-url")) { url = ra.InArgs.GetFirstValue("-url"); } if (ra.InArgs.ContainsKey("-dest")) { ra.State.DestinationDir = ra.InArgs.GetFirstValue("-dest"); } if (ra.InArgs.ContainsKey("-rps")) { reqPerSec = double.Parse(ra.InArgs.GetFirstValue("-rps")); } if (ra.InArgs.ContainsKey("-from")) { fromIdx = int.Parse(ra.InArgs.GetFirstValue("-from")); } if (ra.InArgs.ContainsKey("-to")) { toIdx = int.Parse(ra.InArgs.GetFirstValue("-to")); } } string[] links = null; if (!string.IsNullOrWhiteSpace(url)) { links = new string[] { url } } ; else { links = File.ReadAllLines(linksFile); } if (toIdx < 0) { toIdx = links.Length; } if (fromIdx > 0 && fromIdx > links.Length) { throw new ArgumentOutOfRangeException("from"); } if (toIdx > 0 && (toIdx > links.Length || toIdx <= fromIdx)) { throw new ArgumentOutOfRangeException("to"); } links = links.Skip(fromIdx).Take(toIdx - fromIdx).ToArray(); var LINKS_COUNT = links.Length; var c = 0; var counter = new CountdownEvent(LINKS_COUNT); var startTime = DateTime.Now; if (links != null && LINKS_COUNT > 0) { for (int i = 0; i < LINKS_COUNT; i++) { var rps = i / DateTime.Now.Subtract(startTime).TotalSeconds; while (rps > reqPerSec) { Thread.Sleep(100); rps = i / DateTime.Now.Subtract(startTime).TotalSeconds; } new Task(async(o) => { var idx = (int)o; Uri uri = null; var fn = string.Empty; try { using (var webClient = new HttpClient()) { var link = links[idx]; if (!string.IsNullOrEmpty(baseUrl)) { uri = new Uri(string.Format("{0}/{1}", baseUrl, link)); fn = Path.Combine(ra.State.DestinationDir, link); } else { uri = new Uri(link); fn = Path.Combine(ra.State.DestinationDir, uri.Segments[uri.Segments.Length - 1]); } var bytes = await webClient.GetByteArrayAsync(uri); using (var fs = new FileStream( fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, bytes.Length, true)) await fs.WriteAsync(bytes, 0, bytes.Length); string.Format("{0, 6}/{1}| {2, 6}|K {3}", Interlocked.Increment(ref c), LINKS_COUNT, bytes.Length / 1000, fn).PrintInfo(true); } } catch (Exception ex) { $"@ link {links[idx]}".PrintSysError(true); ex.Message.PrintSysError(true); } finally { counter.Signal(); } }, i).Start(); } } counter.Wait(); var dur = DateTime.Now.Subtract(startTime); $"{Environment.NewLine}Done [{dur.Hours}h {dur.Minutes}m {dur.Seconds}s].".PrintInfo(true); return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); DirectoryInfo root = null; string prog = null; string progArgs = null; string cdf = null; string cd = null; bool recursive = false; if (interactive) { string src = null; Utils.ReadString("Source dir (current): ", ref src); root = !string.IsNullOrEmpty(src) ? new DirectoryInfo(src) : ra.RootDir; Utils.ReadString("Program to run for each dir: ", ref prog, true); recursive = Utils.ReadWord("Recursive dir traversal? (y/*):", "y"); Utils.ReadString("Program arguments: ", ref progArgs, true); Utils.ReadString("Argument string to be replaced by the full current dir path: ", ref cdf); Utils.ReadString("Argument string to be replaced by the current dir name: ", ref cd); } else { root = new DirectoryInfo(ra.InArgs.GetFirstValue("-root")); prog = ra.InArgs.GetFirstValue("-proc"); progArgs = ra.InArgs.GetFirstValue("-pargs"); if (ra.InArgs.ContainsKey("-cdf")) { cdf = ra.InArgs.GetFirstValue("-cdf"); } if (ra.InArgs.ContainsKey("-cd")) { cd = ra.InArgs.GetFirstValue("-cd"); } if (ra.InArgs.ContainsKey("-rec")) { recursive = true; } } foreach (var dir in root.EnumerateDirectories("*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)) { try { var pargs = progArgs; if (!string.IsNullOrWhiteSpace(cdf)) { pargs = pargs.Replace(cdf, "\"" + dir.FullName + "\""); } if (!string.IsNullOrWhiteSpace(cd)) { pargs = pargs.Replace(cd, dir.Name); } var proc = new ProcessStartInfo(prog, pargs); $"Starting {prog} {pargs}".PrintLine(); using (var process = Process.Start(proc)) process.WaitForExit(); } catch (Exception ex) { ex.Message.PrintLine(ConsoleColor.Red); } } return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var tf = string.Empty; var dst = string.Empty; var allText = string.Empty; var quiet = false; var move = false; if (interactive) { Utils.ReadString("File: ", ref tf, true); allText = File.ReadAllText(tf); Utils.ReadString("Destination dir: ", ref dst, true); var q = string.Empty; var m = string.Empty; move = Utils.ReadWord("Move? The default mode is copy (y/*): ", "y"); quiet = Utils.ReadWord("Quiet mode (y/*): ", "y"); } else { if (ra.InArgs.ContainsKey("-f")) { allText = File.ReadAllText(ra.InArgs.GetFirstValue("-f")); } else { throw new ArgumentNullException("-f"); } if (ra.InArgs.ContainsKey("-dst")) { dst = ra.InArgs.GetFirstValue("-dst"); } else { throw new ArgumentNullException("-dst"); } if (ra.InArgs.ContainsKey("-q")) { quiet = true; } if (ra.InArgs.ContainsKey("-m")) { move = true; } } if (!Directory.Exists(dst)) { Directory.CreateDirectory(dst); } var victims = allText.Split(Environment.NewLine); var counter = 0; foreach (var line in victims) { if (line.Length > 0) { if (File.Exists(line)) { var fn = Path.GetFileName(line); if (move) { File.Move(line, Path.Combine(dst, fn)); } else { File.Copy(line, Path.Combine(dst, fn)); } counter++; } else if (!quiet) { $"File not found: {line}".PrintLine(); } } } $"Done, {counter} files were copied/moved.".PrintLine(); return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var allText = string.Empty; var outFile = string.Empty; var fileMap = string.Empty; var newLinefileSep = false; if (interactive) { Utils.ReadString("Out file path: ", ref outFile, true); Utils.ReadString("The file paths map: ", ref fileMap, true); var newLine = string.Empty; Utils.ReadString("Add new line before each file? (y/*): ", ref newLine); newLinefileSep = newLine == "y"; } else { if (ra.InArgs.ContainsKey("-out")) { outFile = ra.InArgs.GetFirstValue("-out"); } else { throw new ArgumentNullException("-out"); } if (ra.InArgs.ContainsKey("-files")) { fileMap = ra.InArgs.GetFirstValue("-files"); } else { throw new ArgumentNullException("-files"); } if (ra.InArgs.ContainsKey("-fsep")) { newLinefileSep = true; } } var sb = new StringBuilder(); var F = File.ReadAllLines(fileMap); var counter = 0; foreach (var f in F) { var t = f.Trim(); if (t.Length > 0) { if (!File.Exists(t)) { throw new FileNotFoundException($"{t}"); } else { if (counter > 0 && newLinefileSep) { sb.Append(Environment.NewLine); } sb.Append(File.ReadAllText(t)); counter++; } } } File.WriteAllText(outFile, sb.ToString()); $"Done {counter} files concatenated.".PrintInfo(); return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var inf = string.Empty; var outf = string.Empty; var colsText = string.Empty; var allText = string.Empty; var sep = COMMA; var cols = new List <int>(); string[] C = null; if (interactive) { Utils.ReadString("File: ", ref inf, true); Utils.ReadString("Out file: ", ref outf, true); Utils.ReadString("Column indices: ", ref colsText, true); Utils.ReadString($"Separator ({COMMA}): ", ref sep); allText = File.ReadAllText(inf); C = colsText.Split(COMMA); } else { if (ra.InArgs.ContainsKey("-in")) { allText = File.ReadAllText(ra.InArgs.GetFirstValue("-in")); } else { throw new ArgumentNullException("-in"); } if (ra.InArgs.ContainsKey("-out")) { outf = ra.InArgs.GetFirstValue("-out"); } else { throw new ArgumentNullException("-out"); } if (ra.InArgs.ContainsKey("-cols")) { var x = ra.InArgs["-cols"]; C = x.Count < 2 ? x[0].Split(COMMA) : x.ToArray(); } else { throw new ArgumentNullException("-cols"); } if (ra.InArgs.ContainsKey("-sep")) { sep = ra.InArgs.GetFirstValue("-sep"); } } foreach (var c in C) { if (!string.IsNullOrWhiteSpace(c)) { var cc = c.TrimEnd(','); cols.Add(int.Parse(cc)); } } var L = allText.Split(Environment.NewLine); var outsb = new StringBuilder(); var counter = 0; foreach (var line in L) { if (line.Length > 0) { var S = line.Split(sep); outsb.AppendLine(); for (int i = 0; i < cols.Count; i++) { if (cols[i] < S.Length) { outsb.Append(S[cols[i]]); if (i < cols.Count - 1) { outsb.Append(sep); } } } counter++; } } File.WriteAllText(outf, outsb.ToString()); $"Done, {counter}/{L.Length} lines were copied.".PrintLine(); return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); if (interactive) { var src = string.Empty; Utils.ReadString("source dir: ", ref src); if (!string.IsNullOrEmpty(src)) { ra.RootDir = new DirectoryInfo(src); } Utils.ReadString("search pattern (*.*): ", ref ra.State.SearchPattern); Utils.ReadString("ext: ", ref ra.State.Ext); } else { if (ra.InArgs.ContainsKey("-src")) { ra.RootDir = new DirectoryInfo(ra.InArgs.GetFirstValue("-src")); } if (ra.InArgs.ContainsKey("-sp")) { ra.State.SearchPattern = ra.InArgs.GetFirstValue("-sp"); } ra.State.Ext = ra.InArgs.GetFirstValue("-ext"); } ra.State.Files = ra.RootDir.GetFiles(ra.State.SearchPattern, SearchOption.AllDirectories); string.Format("There are {0} matches.", ra.State.Files.Length).PrintLine(); ra.Trace = interactive ? Utils.ReadWord("Trace first? (y/*): ", "y") : true; if (ra.Trace) { foreach (var f in ra.State.Files) { if (f.FullName == ra.Me) { continue; } var newpath = Path.ChangeExtension(f.FullName, ra.State.Ext); string.Format(f.FullName + " --> " + newpath).PrintLine(ConsoleColor.Yellow); } } if (!interactive || (interactive && Utils.ReadWord("Rename all? (y/*): ", "y"))) { foreach (var f in ra.State.Files) { var newpath = Path.ChangeExtension(f.FullName, ra.State.Ext); File.Move(f.FullName, newpath); } $"Done - {ra.State.Files.Length} files renamed.".PrintLine(); } else { "Aborting ext.".PrintLine(); } return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var newSrc = string.Empty; var logfilePath = string.Empty; var copy = false; var recursive = true; if (interactive) { Utils.ReadString("Source dir [default is local]: ", ref newSrc); Utils.ReadString("search pattern (*.*): ", ref ra.State.SearchPattern); Utils.ReadString("Log file path: ", ref logfilePath, true); recursive = Utils.ReadWord("Recursive search? (y/*): ", "y"); copy = Utils.ReadWord("Copy? [default is move] (y/*): ", "y"); } else { if (ra.InArgs.ContainsKey("-map")) { logfilePath = ra.InArgs.GetFirstValue("-map"); } else { throw new ArgumentNullException("-map"); } if (ra.InArgs.ContainsKey("-src")) { newSrc = ra.InArgs.GetFirstValue("-src"); } if (ra.InArgs.ContainsKey("-flt")) { ra.State.SearchPattern = ra.InArgs.GetFirstValue("-flt"); } recursive = !ra.InArgs.ContainsKey("-nrec"); copy = ra.InArgs.ContainsKey("-copy"); } if (!string.IsNullOrEmpty(newSrc)) { ra.ChangeRoot(newSrc); } var paths = File.ReadAllLines(logfilePath); var so = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; var allFiles = Directory.GetFiles(ra.RootDir.FullName, ra.State.SearchPattern, so); var origMap = new Dictionary <string, string>(); var localMap = new Dictionary <string, string>(); foreach (var path in paths) { var jn = Path.GetFileName(path); if (!origMap.ContainsKey(jn)) { origMap.Add(jn, path); } else { $"Log file name duplicate {jn}".Print(ConsoleColor.Yellow, null, true); } } foreach (var path in allFiles) { var jn = Path.GetFileName(path); if (!localMap.ContainsKey(jn)) { localMap.Add(jn, path); } else { $"Local file name duplicate {jn}".Print(ConsoleColor.Yellow, null, true); } } var list = new List <string>(localMap.Keys.Intersect(origMap.Keys)); $"{list.Count} files match by name.".PrintLine(); if (list.Count > 0) { if (interactive && Utils.ReadWord("See them? (y/*): ", "y")) { foreach (var fn in list) { $"{localMap[fn]} -> {origMap[fn]}".PrintLine(); } } if (!interactive || Utils.ReadWord("Restore? (y/*): ", "y")) { foreach (var fn in list) { if (copy) { File.Copy(localMap[fn], origMap[fn], true); } else { File.Move(localMap[fn], origMap[fn], true); } } } else { "Aborting".PrintLine(); } } "Done.".PrintLine(); return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var copy = true; var recursive = SearchOption.TopDirectoryOnly; var mirrorDir = false; var ignoreST = 0; var ignoreBT = int.MaxValue / 1000; var regx = string.Empty; if (interactive) { var src = string.Empty; Utils.ReadString("Source dir: ", ref src); if (!string.IsNullOrEmpty(src)) { ra.RootDir = new DirectoryInfo(src); } Utils.ReadString("Destination dir: ", ref ra.State.DestinationDir, true); Utils.ReadString("Search pattern (*.*): ", ref ra.State.SearchPattern); Utils.ReadInt("Ignore files smaller than (in KB): ", ref ignoreST); Utils.ReadInt("Ignore files bigger than (in KB): ", ref ignoreBT); Utils.ReadString("Path matching regex: ", ref regx); Utils.ReadString("Taken files prefix: ", ref ra.State.Prefix); if (Utils.ReadWord("Recursive? (y/*): ", "y")) { recursive = SearchOption.AllDirectories; } if (recursive == SearchOption.AllDirectories && Utils.ReadWord("Mirror dir tree? (y/*): ", "y")) { mirrorDir = true; } copy = !Utils.ReadWord("Move? (y/*): ", "y"); } else { if (ra.InArgs.ContainsKey("-src")) { ra.RootDir = new DirectoryInfo(ra.InArgs.GetFirstValue("-src")); } ra.State.DestinationDir = ra.InArgs.GetFirstValue("-dest"); if (ra.InArgs.ContainsKey("-sp")) { ra.State.SearchPattern = ra.InArgs.GetFirstValue("-sp"); } if (ra.InArgs.ContainsKey("-prf")) { ra.State.Prefix = ra.InArgs.GetFirstValue("-prf"); } ra.State.Take = int.Parse(ra.InArgs.GetFirstValue("-take")); if (ra.InArgs.ContainsKey("-r")) { recursive = SearchOption.AllDirectories; if (ra.InArgs.ContainsKey("-mt")) { mirrorDir = true; } } if (ra.InArgs.ContainsKey("-ist")) { ignoreST = int.Parse(ra.InArgs.GetFirstValue("-ist")); } if (ra.InArgs.ContainsKey("-ibt")) { ignoreBT = int.Parse(ra.InArgs.GetFirstValue("-ibt")); } if (ra.InArgs.ContainsKey("-reg")) { regx = ra.InArgs.GetFirstValue("-reg"); } if (ra.InArgs.ContainsKey("-move")) { copy = false; } } if (ignoreST > ignoreBT || ignoreBT < 1 || ignoreST < 0) { throw new ArgumentException(); } ignoreST *= 1000; ignoreBT *= 1000; if (!Directory.Exists(ra.State.DestinationDir)) { Directory.CreateDirectory(ra.State.DestinationDir); } ra.State.Files = ra.RootDir.GetFiles(ra.State.SearchPattern, recursive) .Where(x => x.Length > ignoreST && x.Length < ignoreBT && (string.IsNullOrEmpty(regx) || Regex.IsMatch(x.FullName, regx))) .Select(x => x).ToArray(); if (interactive) { Utils.ReadInt(string.Format("There are {0} matches. take (0): ", ra.State.Files.Length), ref ra.State.Take); } ra.Trace = interactive ? Utils.ReadWord("Trace first? (y/*): ", "y") : true; List <int> R = Utils.Randomize(ra.State.Files.Length); if (ra.State.Files.Length < ra.State.Take) { ra.State.Take = ra.State.Files.Length; } if (ra.Trace) { for (var i = 0; i < ra.State.Take; i++) { var j = R[i]; var f = ra.State.Files[j]; if (f.FullName == ra.Me) { continue; } var newname = mirrorDir ? Path.Combine( f.Directory.FullName.Replace( ra.RootDir.FullName, string.Empty), ra.State.Prefix + f.Name) .TrimStart(Path.DirectorySeparatorChar) : string.Format("{0}{1}", ra.State.Prefix, f.Name); string.Format("{0} -->{1}{2}{1}", f.FullName, Environment.NewLine, Path.Combine(ra.State.DestinationDir, newname)) .PrintLine(ConsoleColor.Yellow); } } if (!interactive || (interactive && Utils.ReadWord("Take? (y/*): ", "y"))) { var FI = new List <FileInfo>(); for (var i = 0; i < ra.State.Take; i++) { var j = R[i]; var f = ra.State.Files[j]; var newname = mirrorDir ? Path.Combine( f.Directory.FullName.Replace( ra.RootDir.FullName, string.Empty), ra.State.Prefix + f.Name) .TrimStart(Path.DirectorySeparatorChar) : string.Format("{0}{1}", ra.State.Prefix, f.Name); var newpath = Path.Combine(ra.State.DestinationDir, newname); if (!Directory.Exists(newpath)) { Directory.CreateDirectory(Path.GetDirectoryName(newpath)); } if (copy) { File.Copy(f.FullName, newpath); } else { File.Move(f.FullName, newpath); } FI.Add(new FileInfo(newpath)); } ra.State.Files = FI.ToArray(); $"Done - {ra.State.Take} files copied.".PrintLine(); } else { "Aborting take.".PrintLine(); } return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var text = string.Empty; var allText = string.Empty; var tf = string.Empty; var outfile = string.Empty; var except = false; if (interactive) { Utils.ReadString("File: ", ref tf, true); Utils.ReadString("Search text: ", ref text); allText = File.ReadAllText(tf); Utils.ReadString("Output file: ", ref outfile, true); var x = string.Empty; Utils.ReadString("Take all but the matched lines [default is no] (y/*): ", ref x); except = x == "y"; } else { if (ra.InArgs.ContainsKey("-tf")) { allText = File.ReadAllText(ra.InArgs.GetFirstValue("-tf")); } else { throw new ArgumentNullException("-tf"); } if (ra.InArgs.ContainsKey("-text")) { text = ra.InArgs.GetFirstValue("-text"); } else { throw new ArgumentNullException("-text"); } if (ra.InArgs.ContainsKey("-out")) { outfile = ra.InArgs.GetFirstValue("-out"); } else { throw new ArgumentNullException("-out"); } if (ra.InArgs.ContainsKey("-x")) { except = true; } } var lines = allText.Split(Environment.NewLine); var outLines = new StringBuilder(); var counter = 0; foreach (var line in lines) { if (line.Length > 0) { if (line.Contains(text)) { if (!except) { outLines.AppendLine(line); counter++; } } else if (except) { outLines.AppendLine(line); counter++; } } } File.WriteAllText(outfile, outLines.ToString().Trim()); $"Done, {counter} matching lines.".PrintLine(); return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var srcDirs = new List <DirectoryInfo>(); var srcs = string.Empty; var skipLessThanSize = 1; var skipMoreThanSize = -1; var ignExt = string.Empty; var iExt = new List <string>(); var compHash = false; var recursive = false; var inParallel = 1; var delete = false; var saveas = string.Empty; var saveType = string.Empty; ra.State.SearchPattern = "*.*"; if (interactive) { Utils.ReadString("Enter folders to search into, separated by semicolon: ", ref srcs, true); Utils.ReadString("Search pattern (*.*): ", ref ra.State.SearchPattern); recursive = !Utils.ReadWord("Recursive search? (n/*): ", "n"); Utils.ReadInt($"Skip if size < 1Kb: ", ref skipLessThanSize, false); Utils.ReadInt($"Skip if size > #Kb: ", ref skipMoreThanSize, false); Utils.ReadString("Skip extensions (.xyz): ", ref ignExt, false); compHash = Utils.ReadWord("Compare file names (default) or MD5 hashes? (h/*): ", "h"); } else { if (ra.InArgs.ContainsKey("-dirs")) { srcs = ra.InArgs.GetFirstValue("-dirs"); } else { throw new ArgumentNullException("-dirs"); } if (ra.InArgs.ContainsKey("-sp")) { ra.State.SearchPattern = ra.InArgs.GetFirstValue("-sp"); } if (ra.InArgs.ContainsKey("-rec")) { recursive = true; } if (ra.InArgs.ContainsKey("-j")) { inParallel = int.Parse(ra.InArgs.GetFirstValue("-j")); } if (ra.InArgs.ContainsKey("-hash")) { compHash = true; } if (ra.InArgs.ContainsKey("-del")) { delete = true; } if (ra.InArgs.ContainsKey("-save")) { saveas = ra.InArgs.GetFirstValue("-save"); saveType = Path.GetExtension(saveas).Replace(".", ""); } } if (!string.IsNullOrEmpty(ignExt)) { foreach (var ext in ignExt.Split(';')) { iExt.Add(ext.Trim()); } } var useStreamReduction = false; var take = 4000; var skip = 10000; if (compHash && interactive) { Utils.ReadInt($"Concurrent readers (1-{Environment.ProcessorCount}): ", ref inParallel); "By default the hash is computed over the whole file.".PrintLine(); "You can use skip and take parameters to read a portion of the file.".PrintLine(); useStreamReduction = Utils.ReadWord("Do you want to use skip/take? (y/*): ", "y"); if (useStreamReduction) { "The reader always starts with a TAKE (position 0).".PrintLine(); Utils.ReadInt($"Take bytes ({take}): ", ref take, false); Utils.ReadInt($"Skip bytes ({skip}): ", ref skip, false); if (skip < 0 || take < 0) { throw new ArgumentOutOfRangeException("Negative skip or take value."); } } } if (inParallel < 1 || inParallel > Environment.ProcessorCount) { throw new ArgumentOutOfRangeException("inParallel", $"The concurrent readers should be between 0 and {Environment.ProcessorCount}"); } foreach (var p in srcs.Split(';')) { if (Directory.Exists(p)) { srcDirs.Add(new DirectoryInfo(p)); } else { throw new DirectoryNotFoundException(p); // will throw if the path is invalid } } var All = new List <List <FileInfo> >(); var searchOpt = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; // Some subdirectories may be restricted and the GetFiles will throw with AccesDenied // so loop the individual folders separately. foreach (var d in srcDirs) { try { foreach (var sd in d.EnumerateDirectories("*", searchOpt).Union(new DirectoryInfo[] { d })) { try { var L = new List <FileInfo>(); foreach (var file in sd.EnumerateFiles(ra.State.SearchPattern, SearchOption.TopDirectoryOnly)) { if (iExt.Count > 0 && iExt.Contains(file.Extension)) { continue; } if (skipLessThanSize > 0 && file.Length < skipLessThanSize) { continue; } if (skipMoreThanSize > 0 && file.Length > skipMoreThanSize) { continue; } L.Add(file); } All.Add(L); } catch (Exception ex) { ex.Message.PrintLine(ConsoleColor.Red); } } } catch (Exception ex) { ex.Message.PrintLine(ConsoleColor.Red); } } var dict = new Dictionary <string, List <FileInfo> >(); var sb = new StringBuilder(); var totalFiles = 0; var totalFilesChecked = 0; // In all cases the length will be the same foreach (var src in All) { foreach (var fi in src) { var key = $"{fi.Length}"; if (!dict.ContainsKey(key)) { dict.Add(key, new List <FileInfo>()); } else { totalFiles++; } totalFilesChecked++; dict[key].Add(fi); } } totalFiles += totalFiles; // Either compare the names or the data hashes var hashDict = new Dictionary <string, List <FileInfo> >(); var totalDuplicates = 0; var counter = 0; var hashDictSpin = new SpinLock(); $"{Environment.NewLine}Total files checked: {totalFilesChecked}".PrintLine(); Console.CursorVisible = false; var cursorTop = Console.CursorTop; foreach (var kv in dict) { if (kv.Value.Count > 1) { if (compHash) { Console.SetCursorPosition(0, cursorTop); Parallel.For(0, kv.Value.Count, new ParallelOptions() { MaxDegreeOfParallelism = inParallel }, (i) => { try { Stream stream = File.OpenRead(kv.Value[i].FullName); if (useStreamReduction) { stream = new StreamReductor(stream, skip, take); } using (var md5 = MD5.Create()) using (stream) { $"{++counter}/{totalFiles} [{kv.Value[i].Length / 1000}]Kb file{i} = {kv.Value[i].FullName}".PrintLine(ConsoleColor.Yellow); var h = md5.ComputeHash(stream); var key = BitConverter.ToString(h); var acq = false; hashDictSpin.Enter(ref acq); if (acq) { if (!hashDict.ContainsKey(key)) { hashDict.Add(key, new List <FileInfo>()); } else { totalDuplicates++; } hashDict[key].Add(kv.Value[i]); hashDictSpin.Exit(); } } } catch (Exception ex) { ex.Message.PrintSysError(); return; } }); var cursorTopNow = Console.CursorTop; for (int top = cursorTop; top < cursorTopNow; top++) { for (int left = 0; left < Console.WindowWidth; left++) { Console.SetCursorPosition(left, top); " ".Print(); } } } else { foreach (var f in kv.Value) { var key = f.Name.ToLowerInvariant(); if (!hashDict.ContainsKey(key)) { hashDict.Add(key, new List <FileInfo>()); } else { totalDuplicates++; } hashDict[key].Add(f); $"Comparing {++counter}/{totalFiles}".Print(ConsoleColor.Yellow); Console.SetCursorPosition(0, Console.CursorTop); } } } } Console.SetCursorPosition(0, cursorTop); Console.CursorVisible = true; sb.AppendLine("Files with the same length and name/hash are grouped together."); sb.AppendLine(); foreach (var kv in hashDict) { if (kv.Value.Count > 1) { var size = kv.Value[0].Length / 1000; string lbl = size < 1 ? lbl = $"{kv.Value[0].Length}b" : $"{size}Kb"; sb.AppendLine($"[{kv.Key}] {lbl}"); foreach (var p in kv.Value) { sb.AppendLine(p.FullName); } sb.AppendLine(); } } $"There are {totalDuplicates} files with clones.".PrintLine(); if (totalDuplicates > 0) { ra.Trace = interactive && Utils.ReadWord("Trace? (y/*): ", "y"); if (ra.Trace) { sb.ToString().PrintLine(ConsoleColor.Yellow); } // In case of -ni var opt = saveType; if (delete || (interactive && Utils.PickOption("Save results? (fdelete, xml, json, txt/*): ", ref opt, false, "fdelete", "xml", "json", "txt"))) { var fn = string.Empty; var data = string.Empty; if (interactive) { Utils.ReadString("Result file path: ", ref fn, true); } else { fn = saveas; } if (opt == "fdelete" || delete) { var toDel = new StringBuilder(); foreach (var kv in hashDict) { if (kv.Value.Count > 1) { foreach (var f in kv.Value.Select(x => x.FullName).Skip(1)) { toDel.AppendLine(f); if (delete && File.Exists(f)) { File.Delete(f); } } } } data = toDel.ToString(); } if (opt == "txt") { data = sb.ToString(); } else { var L = new List <Duplicate>(); foreach (var kv in hashDict) { if (kv.Value.Count > 1) { L.Add(new Duplicate(kv.Key, kv.Value[0].Length, kv.Value.Select(x => x.FullName).ToArray())); } } if (opt == "json") { data = L.ToJson(); } else { data = L.ToXml(); } } if (!string.IsNullOrEmpty(fn)) { File.WriteAllText(fn, data); } } } return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var newSrc = string.Empty; ra.State.SearchPattern = "*.*"; var currentDirOnly = false; var justNames = false; var logOutFile = string.Empty; if (interactive) { Utils.ReadString("Source dir [default is local]: ", ref newSrc); Utils.ReadString("search pattern (*.*): ", ref ra.State.SearchPattern); currentDirOnly = Utils.ReadWord("Recursive? [default is yes] (n/*): ", "n"); justNames = Utils.ReadWord("Log full paths? [default is yes] (n/*): ", "n"); } else { if (ra.InArgs.ContainsKey("-src")) { newSrc = ra.InArgs.GetFirstValue("-src"); } if (ra.InArgs.ContainsKey("-flt")) { ra.State.SearchPattern = ra.InArgs.GetFirstValue("-flt"); } currentDirOnly = ra.InArgs.ContainsKey("-nrec"); justNames = ra.InArgs.ContainsKey("-nfp"); if (ra.InArgs.ContainsKey("-out")) { logOutFile = ra.InArgs.GetFirstValue("-out"); } else { throw new ArgumentNullException("-out"); } } if (!string.IsNullOrEmpty(newSrc)) { ra.ChangeRoot(newSrc); } var F = new List <string>(); foreach (var f in ra.RootDir.GetFiles(ra.State.SearchPattern, currentDirOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories)) { F.Add(justNames ? f.Name : f.FullName); } if (interactive) { string.Format("Found {0} matching files.", F.Count).PrintLine(); if (F.Count > 0) { if (Utils.ReadWord("See them? (y/*): ", "y")) { foreach (var f in F) { f.PrintLine(ConsoleColor.Yellow); } } "Save as (file path): ".Print(); logOutFile = Path.GetFullPath(Console.ReadLine()); } } File.WriteAllLines(logOutFile, F.ToArray()); string.Format("Name file saved as {0}", logOutFile).PrintLine(); "Done.".PrintLine(); return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); int sort = 4; string src = null; ra.Trace = false; ra.State.NameCounter = int.MinValue; if (interactive) { Utils.ReadString("Source dir (current): ", ref src); if (!string.IsNullOrEmpty(src)) { ra.ChangeRoot(src); } Utils.ReadString("Destination dir: ", ref ra.State.DestinationDir, true); Utils.ReadString("Search pattern (*.*): ", ref ra.State.SearchPattern); Utils.ReadString("Prefix: ", ref ra.State.Prefix); Utils.ReadInt("Counter start value (default is prefix only): ", ref ra.State.NameCounter); if (ra.State.NameCounter > int.MinValue) { Utils.ReadInt("Counter step (1): ", ref ra.State.NameCounterStep); Utils.ReadInt("Number length with zero padding (6): ", ref ra.State.PadZeroes); "Sort options: no-0, asc name-1, desc name-2, randomize-3, asc createdate-4, desc createdate-5. ".PrintLine(); Utils.ReadIntIn("Option (4): ", ref sort, new int[] { 0, 1, 2, 3, 4, 5 }); } } else { ra.ChangeRoot(ra.InArgs.GetFirstValue("-src")); ra.State.DestinationDir = ra.InArgs.GetFirstValue("-dest"); if (ra.InArgs.ContainsKey("-sp")) { ra.State.SearchPattern = ra.InArgs.GetFirstValue("-sp"); } if (ra.InArgs.ContainsKey("-prf")) { ra.State.Prefix = ra.InArgs.GetFirstValue("-prf"); } if (ra.InArgs.ContainsKey("-ic")) { ra.State.NameCounter = int.Parse(ra.InArgs.GetFirstValue("-ic")); } if (ra.InArgs.ContainsKey("-step")) { ra.State.NameCounterStep = int.Parse(ra.InArgs.GetFirstValue("-step")); } if (ra.InArgs.ContainsKey("-zpad")) { ra.State.PadZeroes = int.Parse(ra.InArgs.GetFirstValue("-zpad")); } if (ra.InArgs.ContainsKey("-sort")) { ra.State.PadZeroes = int.Parse(ra.InArgs.GetFirstValue("-sort")); } } ra.State.Sort = (SortType)sort; ra.State.Files = ra.RootDir.GetFiles(ra.State.SearchPattern, SearchOption.TopDirectoryOnly); ra.State.SortFiles(ra.State.Sort); string.Format("There are {0} matches.", ra.State.Files.Length).PrintLine(); if (interactive) { ra.Trace = Utils.ReadWord("Trace first? (y/*): ", "y"); } string padded, newpath, newname; if (ra.Trace) { var tcounter = ra.State.NameCounter; foreach (var f in ra.State.Files) { if (f.FullName == ra.Me) { continue; } if (ra.State.NameCounter > int.MinValue) { tcounter += ra.State.NameCounterStep; padded = PadNumbers.PadZeroes(tcounter.ToString(), ra.State.PadZeroes); newname = string.Format("{0}{1}{2}", ra.State.Prefix, padded, f.Extension); } else { newname = ra.State.Prefix + f.Name; } newpath = Path.Combine(ra.State.DestinationDir, newname); string.Format(f.Name + " --> " + newpath).PrintLine(ConsoleColor.Yellow); } } if (!interactive || Utils.ReadWord("Rename all? (y/*): ", "y")) { var FI = new List <FileInfo>(); foreach (var f in ra.State.Files) { if (f.FullName == ra.Me) { continue; } if (ra.State.NameCounter > int.MinValue) { ra.State.IncrementCounter(); padded = PadNumbers.PadZeroes(ra.State.NameCounter.ToString(), ra.State.PadZeroes); newname = string.Format("{0}{1}{2}", ra.State.Prefix, padded, f.Extension); } else { newname = ra.State.Prefix + f.Name; } newpath = Path.Combine(ra.State.DestinationDir, newname); File.Move(f.FullName, newpath); FI.Add(new FileInfo(newpath)); } ra.State.Files = FI.ToArray(); $"Done - {ra.State.Files.Length} files renamed.".PrintLine(); } else { "Aborting move.".PrintLine(); } return(0); }
public int Run(RunArgs ra) { var interactive = !ra.InArgs.ContainsKey("-ni"); var err = ra.InArgs.ContainsKey("-err"); string src = ra.RootDir.FullName; if (interactive) { Utils.ReadString("Source dir (current): ", ref src); Utils.ReadString("Search pattern (*.*): ", ref ra.State.SearchPattern); } else { ra.ChangeRoot(ra.InArgs.GetFirstValue("-src")); if (ra.InArgs.ContainsKey("-sp")) { ra.State.SearchPattern = ra.InArgs.GetFirstValue("-sp"); } } var d = new DirectoryInfo(src); var All = new List <FileInfo>(); var searchOpt = SearchOption.AllDirectories; try { var D = d.EnumerateDirectories("*", searchOpt).GetEnumerator(); foreach (var file in d.EnumerateFiles(ra.State.SearchPattern, SearchOption.TopDirectoryOnly)) { All.Add(file); } while (true) { try { if (!D.MoveNext()) { break; } foreach (var file in D.Current.EnumerateFiles(ra.State.SearchPattern, SearchOption.TopDirectoryOnly)) { All.Add(file); } } catch (Exception ex) { if (err) { ex.Message.PrintLine(ConsoleColor.Red); } } } } catch (Exception ex) { if (err) { ex.Message.PrintLine(ConsoleColor.Red); } } $"{Environment.NewLine}There are {All.Count} matches: {Environment.NewLine}".PrintLine(); foreach (var f in All) { f.FullName.PrintLine(); } "".PrintLine(); return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); DirectoryInfo root = ra.RootDir; string prog = "files"; string progArgs = null; string cdf = null; string cd = null; var recursive = "n"; var ci = 1; var sp = string.Empty; if (interactive) { var src = string.Empty; var cc = string.Empty; Utils.ReadString("Source dir (current): ", ref src); Utils.ReadString("File search pattern (*.*): ", ref sp); Utils.ReadString("Recursive? (y/*): ", ref recursive); root = !string.IsNullOrEmpty(src) ? new DirectoryInfo(src) : ra.RootDir; Utils.ReadString("Program to run for each dir (files): ", ref prog); Utils.ReadString("Concurrent instances (1): ", ref cc); if (!string.IsNullOrWhiteSpace(cc)) { ci = int.Parse(cc); } Utils.ReadString("Program arguments: ", ref progArgs, true); Utils.ReadString("Argument string to be replaced by the full current dir path: ", ref cdf); Utils.ReadString("Argument string to be replaced by the current dir name: ", ref cd); } else { if (ra.InArgs.ContainsKey("-root")) { root = new DirectoryInfo(ra.InArgs.GetFirstValue("-root")); } if (ra.InArgs.ContainsKey("-proc")) { prog = ra.InArgs.GetFirstValue("-proc"); } progArgs = ra.InArgs.GetFirstValue("-pargs"); if (ra.InArgs.ContainsKey("-sp")) { sp = ra.InArgs.GetFirstValue("-sp"); } if (ra.InArgs.ContainsKey("-rec")) { recursive = ra.InArgs.GetFirstValue("-rec"); } if (ra.InArgs.ContainsKey("-cdf")) { cdf = ra.InArgs.GetFirstValue("-cdf"); } if (ra.InArgs.ContainsKey("-cd")) { cd = ra.InArgs.GetFirstValue("-cd"); } if (ra.InArgs.ContainsKey("-ci")) { ci = int.Parse(ra.InArgs.GetFirstValue("-ci")); } } var so = recursive == "y" ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; var F = new List <FileInfo>(root.EnumerateFiles(sp, so)); Parallel.For(0, F.Count, new ParallelOptions() { MaxDegreeOfParallelism = ci }, (i) => { try { var f = F[i]; var pargs = progArgs; if (!string.IsNullOrWhiteSpace(cdf)) { pargs = pargs.Replace(cdf, "\"" + f.FullName + "\""); } if (!string.IsNullOrWhiteSpace(cd)) { pargs = pargs.Replace(cd, f.Directory.FullName); } var proc = new ProcessStartInfo(prog, pargs); $"Starting {prog} {pargs}".PrintLine(); using (var process = Process.Start(proc)) process.WaitForExit(); } catch (Exception ex) { ex.Message.PrintLine(ConsoleColor.Red); ex.StackTrace.PrintSysWarn(); } }); return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var end = false; var ovr = false; var text = string.Empty; var tf = string.Empty; var recursive = SearchOption.TopDirectoryOnly; var ignoreRegx = string.Empty; if (interactive) { var src = string.Empty; Utils.ReadString("Source folder: ", ref src); if (!string.IsNullOrEmpty(src)) { ra.RootDir = new DirectoryInfo(src); } Utils.ReadString("Search pattern (*.*): ", ref ra.State.SearchPattern); Utils.ReadString("Ignore regex: ", ref ignoreRegx); Utils.ReadString("Text: ", ref text); if (string.IsNullOrWhiteSpace(text)) { Utils.ReadString("Text file: ", ref tf, true); text = File.ReadAllText(tf); } if (Utils.ReadWord("Recursive? (y/*): ", "y")) { recursive = SearchOption.AllDirectories; } ovr = (Utils.ReadWord("Override? (y/*): ", "y")); end = (Utils.ReadWord("At the end of the file? (y/*): ", "y")); } else { if (ra.InArgs.ContainsKey("-src")) { ra.RootDir = new DirectoryInfo(ra.InArgs.GetFirstValue("-src")); } if (ra.InArgs.ContainsKey("-sp")) { ra.State.SearchPattern = ra.InArgs.GetFirstValue("-sp"); } if (ra.InArgs.ContainsKey("-ireg")) { ignoreRegx = ra.InArgs.GetFirstValue("-ireg"); } if (ra.InArgs.ContainsKey("-text")) { text = ra.InArgs.GetFirstValue("-text"); } else { if (ra.InArgs.ContainsKey("-tf")) { text = File.ReadAllText(ra.InArgs.GetFirstValue("-tf")); } else { throw new ArgumentNullException("-tf"); } } if (ra.InArgs.ContainsKey("-r") && ra.InArgs.GetFirstValue("-r") == "y") { recursive = SearchOption.AllDirectories; } end = ra.InArgs.ContainsKey("-append"); ovr = ra.InArgs.ContainsKey("-ovr"); } var F = new List <FileInfo>(); var I = new List <FileInfo>(); foreach (var file in ra.RootDir.EnumerateFiles(ra.State.SearchPattern, recursive)) { if (!string.IsNullOrEmpty(ignoreRegx) && Regex.IsMatch(file.FullName, ignoreRegx)) { I.Add(file); } else { F.Add(file); } } $"There are {F.Count + I.Count} files, {I.Count} ignored by a regex match".PrintLine(ConsoleColor.Yellow); if (F.Count > 0) { if (interactive && Utils.ReadWord("Trace first? (y/*): ", "y")) { $"{Environment.NewLine}Files:".PrintLine(); foreach (var f in F) { $"{f.FullName}".PrintLine(ConsoleColor.DarkYellow); } $"{Environment.NewLine}Ignored files:".PrintLine(); foreach (var f in I) { $"{f.FullName}".PrintLine(ConsoleColor.DarkRed); } } "".PrintLine(); if (interactive && !Utils.ReadWord("Edit? (y/*): ", "y")) { return(-1); } "".PrintLine(); var c = 0; foreach (var f in F) { try { Utils.WriteToFile(f.FullName, text, ovr, end); var pl = string.Format("{0, 6}: {1}", ++c, f.FullName); pl.PrintLine(ConsoleColor.Green); } catch (Exception ex) { $"{f.FullName} ex: {ex.Message}".PrintSysError(); } } } $"{Environment.NewLine}Done. {Environment.NewLine}".PrintLine(ConsoleColor.Yellow); return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var end = false; var text = string.Empty; var tf = string.Empty; var allText = string.Empty; var outfile = string.Empty; if (interactive) { Utils.ReadString("Text to include: ", ref text); Utils.ReadString("File: ", ref tf, true); allText = File.ReadAllText(tf); Utils.ReadString("Output file: ", ref outfile, true); end = (Utils.ReadWord("Append? (y/*): ", "y")); } else { if (ra.InArgs.ContainsKey("-text")) { text = ra.InArgs.GetFirstValue("-text"); } else { throw new ArgumentNullException("-text"); } if (ra.InArgs.ContainsKey("-tf")) { allText = File.ReadAllText(ra.InArgs.GetFirstValue("-tf")); } else { throw new ArgumentNullException("-tf"); } if (ra.InArgs.ContainsKey("-out")) { outfile = ra.InArgs.GetFirstValue("-out"); } else { throw new ArgumentNullException("-out"); } end = ra.InArgs.ContainsKey("-append"); } var lines = allText.Split(Environment.NewLine); var outLines = new StringBuilder(); foreach (var line in lines) { if (line.Length < 1) { continue; } if (end) { outLines.AppendLine(line + text); } else { outLines.AppendLine(text + line); } } File.WriteAllText(outfile, outLines.ToString()); "Done.".PrintLine(); return(0); }
public int Run(RunArgs ra) { "The Log can:".PrintLine(); " (log) save the file names in the current dir".PrintLine(); " (restore) move the files from a log file to a destination dir".PrintLine(); var mode = ""; while (mode != "log" && mode != "restore") { Utils.ReadString("mode (log) or (restore): ", ref mode, true); } var newSrc = string.Empty; Utils.ReadString("Source dir [default is local]: ", ref newSrc); if (!string.IsNullOrEmpty(newSrc)) { ra.ChangeRoot(newSrc); } Utils.ReadString("search pattern (*.*): ", ref ra.State.SearchPattern); if (mode == "log") { var currentDir = Utils.ReadWord("Recursive? [default is yes] (n/*): ", "n"); var justNames = Utils.ReadWord("Log full paths? [default is yes] (n/*): ", "n"); var F = new List <string>(); foreach (var f in ra.RootDir.GetFiles(ra.State.SearchPattern, currentDir ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories)) { F.Add(justNames ? f.Name : f.FullName); } string.Format("Found {0} matching files.", F.Count).PrintLine(); if (F.Count > 0) { if (Utils.ReadWord("See them? (y/*): ", "y")) { foreach (var f in F) { f.PrintLine(ConsoleColor.Yellow); } } "Save as (file path): ".Print(); var p = Path.GetFullPath(Console.ReadLine()); File.WriteAllLines(p, F.ToArray()); string.Format("Name file saved as {0}", p).PrintLine(); } "Done.".PrintLine(); } else { var logfilePath = string.Empty; Utils.ReadString("Log file path: ", ref logfilePath, true); var paths = File.ReadAllLines(logfilePath); var recursive = Utils.ReadWord("Recursive search? (y/*): ", "y"); var copy = Utils.ReadWord("Copy? [default is move] (y/*): ", "y"); var so = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; var allFiles = Directory.GetFiles(ra.RootDir.FullName, ra.State.SearchPattern, so); var origMap = new Dictionary <string, string>(); var localMap = new Dictionary <string, string>(); foreach (var path in paths) { var jn = Path.GetFileName(path); if (!origMap.ContainsKey(jn)) { origMap.Add(jn, path); } else { $"Log file name duplicate {jn}".Print(ConsoleColor.Yellow, null, true); } } foreach (var path in allFiles) { var jn = Path.GetFileName(path); if (!localMap.ContainsKey(jn)) { localMap.Add(jn, path); } else { $"Local file name duplicate {jn}".Print(ConsoleColor.Yellow, null, true); } } var list = new List <string>(localMap.Keys.Intersect(origMap.Keys)); $"{list.Count} files match by name.".PrintLine(); if (list.Count > 0) { if (Utils.ReadWord("See them? (y/*): ", "y")) { foreach (var fn in list) { $"{localMap[fn]} -> {origMap[fn]}".PrintLine(); } } if (Utils.ReadWord("Restore? (y/*): ", "y")) { foreach (var fn in list) { if (copy) { File.Copy(localMap[fn], origMap[fn], true); } else { File.Move(localMap[fn], origMap[fn], true); } } } else { "Aborting".PrintLine(); } } "Done.".PrintLine(); } return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var allText = string.Empty; var src = string.Empty; var dst = string.Empty; var clones = string.Empty; var linesArePaths = false; if (interactive) { Utils.ReadString("Source file: ", ref src, true); Utils.ReadString("Output file: ", ref dst, true); Utils.ReadString("Clones file: ", ref clones, false); linesArePaths = Utils.ReadWord("Lines are paths? [default is no] (y/*): ", "y"); } else { if (ra.InArgs.ContainsKey("-src")) { src = ra.InArgs.GetFirstValue("-src"); } else { throw new ArgumentNullException("-src"); } if (ra.InArgs.ContainsKey("-dst")) { dst = ra.InArgs.GetFirstValue("-dst"); } else { throw new ArgumentNullException("-dst"); } if (ra.InArgs.ContainsKey("-paths")) { linesArePaths = true; } if (ra.InArgs.ContainsKey("-clones")) { clones = ra.InArgs.GetFirstValue("-clones"); } } allText = File.ReadAllText(src); var allLines = allText.Split(Environment.NewLine); var linesMap = new Dictionary <string, string>(); var clonesList = new List <string>(); for (var i = 0; i < allLines.Length; i++) { var line = allLines[i]; if (linesArePaths) { line = Path.GetFileName(line); } if (!linesMap.ContainsKey(line)) { linesMap.Add(line, allLines[i]); } else { clonesList.Add(allLines[i]); } } var sb = new StringBuilder(); foreach (var line in linesMap.Values) { sb.AppendLine(line); } File.WriteAllText(dst, sb.ToString()); if (!string.IsNullOrWhiteSpace(clones)) { clonesList.Sort(); sb.Clear(); foreach (var line in clonesList) { sb.AppendLine(line); } File.WriteAllText(clones, sb.ToString()); } "Done.".PrintLine(); $"{clonesList.Count} clones found".PrintLine(); return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var tf = string.Empty; var dst = string.Empty; var srcText = string.Empty; var quiet = false; var move = false; var ovr = false; if (interactive) { Utils.ReadString("File: ", ref tf, true); srcText = File.ReadAllText(tf); Utils.ReadString("Destination dir: ", ref dst, true); var q = string.Empty; var m = string.Empty; move = Utils.ReadWord("Move? The default mode is copy (y/*): ", "y"); quiet = Utils.ReadWord("Quiet mode (y/*): ", "y"); ovr = Utils.ReadWord("Override existing files (y/*): ", "y"); } else { if (ra.InArgs.ContainsKey("-f")) { srcText = File.ReadAllText(ra.InArgs.GetFirstValue("-f")); } else { throw new ArgumentNullException("-f"); } if (ra.InArgs.ContainsKey("-dst")) { dst = ra.InArgs.GetFirstValue("-dst"); } else { throw new ArgumentNullException("-dst"); } if (ra.InArgs.ContainsKey("-q")) { quiet = true; } if (ra.InArgs.ContainsKey("-m")) { move = true; } if (ra.InArgs.ContainsKey("-ovr")) { ovr = true; } } var counter = 0; var srcLines = srcText.Split(Environment.NewLine); // If dst is file like -f they must match in lines if (File.Exists(dst)) { var dstText = File.ReadAllText(dst); var dstLines = dstText.Split(Environment.NewLine); if (dstLines.Length != dstLines.Length) { throw new Exception("The source and destination do not match in length."); } for (int i = 0; i < srcLines.Length; i++) { var from = srcLines[i]; var to = dstLines[i]; if (from.Length > 0) { if (File.Exists(from)) { var todir = Path.GetDirectoryName(to); if (!Directory.Exists(todir)) { Directory.CreateDirectory(todir); } if (File.Exists(to) && ovr) { File.Delete(to); } if (!File.Exists(to)) { if (move) { File.Move(from, to); } else { File.Copy(from, to); } counter++; } else if (!quiet) { $"File already exists: {to}".PrintLine(); } } else if (!quiet) { $"File not found: {from}".PrintLine(); } } } } else { if (!Directory.Exists(dst)) { Directory.CreateDirectory(dst); } foreach (var line in srcLines) { if (line.Length > 0) { if (File.Exists(line)) { var fn = Path.GetFileName(line); var to = Path.Combine(dst, fn); if (File.Exists(to) && ovr) { File.Delete(to); } if (!File.Exists(to)) { if (move) { File.Move(line, to); } else { File.Copy(line, to); } counter++; } else if (!quiet) { $"File already exists: {to}".PrintLine(); } } else if (!quiet) { $"File not found: {line}".PrintLine(); } } } } var com = move ? "moved" : "copied"; $"Done, {counter} files were {com}.".PrintLine(); return(0); }
public int Run(RunArgs ra) { Utils.ReadString("the path of the text file with the new file names: ", ref ra.State.FileNamesFilePath, true); Utils.ReadString("destination dir: ", ref ra.State.DestinationDir, true); Utils.ReadString("search pattern (*.*): ", ref ra.State.SearchPattern); Utils.ReadString("prefix: ", ref ra.State.Prefix); Utils.ReadString("name from file prefix: ", ref ra.State.NameFromFilePrefix); Utils.ReadInt("counter (0) : ", ref ra.State.NameCounter); Utils.ReadInt("counter step (1) : ", ref ra.State.NameCounterStep); Utils.ReadInt("number length with zero padding (6) : ", ref ra.State.PadZeroes); "sort options: no-0, asc name-1, desc name-2, randomize-3, asc createdate-4, desc createdate-5. ".PrintLine(); int sort = 0; Utils.ReadIntIn("sort first (no) : ", ref sort, new int[] { 0, 1, 2, 3, 4, 5 }); ra.State.Sort = (SortType)sort; var useexisting = false; if (ra.State.Files != null && ra.State.Files.Length > 0) { string.Format( "There are {0} files from a previous subprogram run. {1}The first of the old files: {2} ", ra.State.Files.Length, Environment.NewLine, ra.State.Files[0]).PrintLine(); useexisting = Utils.ReadWord("Use them? (y/*) ", "y"); } if (!useexisting) { ra.State.Files = ra.RootDir.GetFiles(ra.State.SearchPattern, SearchOption.TopDirectoryOnly); } else if (Utils.ReadWord(string.Format("Change the root dir ({0}) ? (y/*)", ra.RootDir.FullName), "y")) { var newroot = string.Empty; Utils.ReadString("Enter new root dir: ", ref newroot, true); ra.ChangeRoot(newroot); } ra.State.SortFiles(ra.State.Sort); string.Format("There are {0} matches.", ra.State.Files.Length).PrintLine(); ra.Trace = Utils.ReadWord("Trace first? (y/*): ", "y"); var newNames = File.ReadAllLines(ra.State.FileNamesFilePath); if (newNames.Length < ra.State.Files.Length) { var msg = string.Format( "The new names length is {0} and the files to be renamed are {1}. Abort? (y/*)", newNames.Length, ra.State.Files.Length); if (Utils.ReadWord(msg, "y")) { return(-1); } } string.Format("Files - {0}, Names - {1}", newNames.Length, ra.State.Files.Length).PrintLine(ConsoleColor.Yellow); if (ra.Trace) { var tcounter = ra.State.NameCounter; var i = 0; foreach (var f in ra.State.Files) { if (f.FullName == ra.Me) { continue; } tcounter += ra.State.NameCounterStep; var padded = PadNumbers.PadZeroes(tcounter.ToString(), ra.State.PadZeroes); var newname = string.Format("{0}{1}{2}{3}{4}", ra.State.Prefix, padded, ra.State.NameFromFilePrefix, newNames[i], f.Extension); var newpath = Path.Combine(ra.State.DestinationDir, newname); string.Format(f.Name + " --> " + newpath).PrintLine(ConsoleColor.Yellow); i++; } } if (Utils.ReadWord("Rename all? (y/*): ", "y")) { var FI = new List <FileInfo>(); var i = 0; foreach (var f in ra.State.Files) { if (f.FullName == ra.Me) { continue; } ra.State.IncrementCounter(); var padded = PadNumbers.PadZeroes(ra.State.NameCounter.ToString(), ra.State.PadZeroes); var newname = string.Format("{0}{1}{2}{3}{4}", ra.State.Prefix, padded, ra.State.NameFromFilePrefix, newNames[i], f.Extension); var newpath = Path.Combine(ra.State.DestinationDir, newname); File.Move(f.FullName, newpath); FI.Add(new FileInfo(newpath)); i++; } ra.State.Files = FI.ToArray(); $"Done - {ra.State.Files.Length} files renamed.".PrintLine(); } else { "Aborting rename.".PrintLine(); } return(0); }
public int Run(RunArgs ra) { bool interactive = !ra.InArgs.ContainsKey("-ni"); var text = string.Empty; var tf = string.Empty; var recursive = SearchOption.TopDirectoryOnly; var ignoreRegx = string.Empty; var targetRegx = string.Empty; if (interactive) { var src = string.Empty; Utils.ReadString("Source folder: ", ref src); if (!string.IsNullOrEmpty(src)) { ra.RootDir = new DirectoryInfo(src); } Utils.ReadString("Search pattern (*.*): ", ref ra.State.SearchPattern); Utils.ReadString("Target regex: ", ref targetRegx); Utils.ReadString("Ignore regex: ", ref ignoreRegx); Utils.ReadString("Text: ", ref text); if (Utils.ReadWord("Recursive? (y/*): ", "y")) { recursive = SearchOption.AllDirectories; } } else { if (ra.InArgs.ContainsKey("-src")) { ra.RootDir = new DirectoryInfo(ra.InArgs.GetFirstValue("-src")); } if (ra.InArgs.ContainsKey("-sp")) { ra.State.SearchPattern = ra.InArgs.GetFirstValue("-sp"); } if (ra.InArgs.ContainsKey("-reg")) { targetRegx = ra.InArgs.GetFirstValue("-reg"); } if (ra.InArgs.ContainsKey("-ireg")) { ignoreRegx = ra.InArgs.GetFirstValue("-ireg"); } if (ra.InArgs.ContainsKey("-text")) { text = ra.InArgs.GetFirstValue("-text"); } if (ra.InArgs.ContainsKey("-r") && ra.InArgs.GetFirstValue("-r") == "y") { recursive = SearchOption.AllDirectories; } } var F = new List <FileInfo>(); var I = new List <FileInfo>(); foreach (var file in ra.RootDir.EnumerateFiles(ra.State.SearchPattern, recursive)) { if (!string.IsNullOrEmpty(ignoreRegx) && Regex.IsMatch(file.FullName, ignoreRegx)) { I.Add(file); } else { F.Add(file); } } $"There are {F.Count + I.Count} files, {I.Count} ignored by a regex match".PrintLine(ConsoleColor.Yellow); if (F.Count > 0) { if (interactive && Utils.ReadWord("Trace first? (y/*): ", "y")) { $"{Environment.NewLine}Files:".PrintLine(); foreach (var f in F) { $"{f.FullName}".PrintLine(ConsoleColor.DarkYellow); } $"{Environment.NewLine}Ignored files:".PrintLine(); foreach (var f in I) { $"{f.FullName}".PrintLine(ConsoleColor.DarkRed); } } "".PrintLine(); if (interactive && !Utils.ReadWord("Edit? (y/*): ", "y")) { return(-1); } "".PrintLine(); var c = 0; text = Regex.Unescape(text); foreach (var f in F) { try { var allBytes = File.ReadAllBytes(f.FullName); var allText = Encoding.UTF8.GetString(allBytes); if (allText != null && allText.Length > 0) { var r = Regex.Replace(allText, targetRegx, text); if (r != null) { allBytes = Encoding.UTF8.GetBytes(r); File.WriteAllBytes(f.FullName, allBytes); } } var pl = string.Format("{0, 6}: {1}", ++c, f.FullName); pl.PrintLine(ConsoleColor.Green); } catch (Exception ex) { $"{f.FullName} ex: {ex.Message}".PrintSysError(); } } } $"{Environment.NewLine}Done. {Environment.NewLine}".PrintLine(ConsoleColor.Yellow); return(0); }