public static void RegexCopy(MyInputInfo myInputInfo) { foreach (string file in myInputInfo.InputValues) { string filePath = Path.GetDirectoryName(file) + '\\'; string fileName = Path.GetFileName(file); GroupCollection groups = Regex.Match(fileName, myInputInfo.Pattern, Multiline).Groups; if (groups.Count <= 1) { continue; } if (myInputInfo.Parameters.Length < (groups.Count - 1)) { ushort tmp = (ushort)((groups.Count - 1) - myInputInfo.Parameters.Length); Console.WriteLine($"Need {tmp} more parameter{(tmp < 1 ? "s" : String.Empty)}"); return; } string result = String.Empty; short previousIndex = 0; for (byte group = 1; group < groups.Count; group++) { result = result + fileName.Substring((result.Length + previousIndex), (groups[group].Index - result.Length - previousIndex)); result = result + myInputInfo.Parameters[group - 1]; previousIndex = (short)(groups[group].Length - myInputInfo.Parameters[group - 1].Length); } previousIndex = (short)(groups[groups.Count - 1].Index + groups[groups.Count - 1].Length); result = result + fileName.Substring(previousIndex, (fileName.Length - previousIndex)); Console.WriteLine($"Output[{i++}]: \"{result}\""); result = filePath + result; File.Copy(file, result); } }
static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine("Usage: Regex.exe <type of action> <pattern> <input directory> <parameters>"); Exit(ExitCode); } myInputInfo = new MyInputInfo(args); Console.WriteLine("--------------------------------------------------"); Console.WriteLine($"Action: {myInputInfo.Action}"); Console.WriteLine($"Pattern: \"{myInputInfo.Pattern}\""); Console.WriteLine($"Input: \"{myInputInfo.InputString}\" ({(myInputInfo.InputType == MyInputInfoType.File ? "File" : "Directory")})"); if (myInputInfo.Parameters == null) { Console.WriteLine("No parameters added"); } else { for (int i = 0; i < myInputInfo.Parameters.Length; i++) { Console.WriteLine($"Parameter[{(i + 1)}]: \"{myInputInfo.Parameters[i]}\""); } } switch (myInputInfo.Action) { case "replace": case "rename": case "move": case "mv": RegexMove(myInputInfo); break; case "copy": case "scp": RegexCopy(myInputInfo); break; case "ls": case "dir": case "find": RegexFind(myInputInfo); break; case "delete": case "del": case "rm": case "remove": RegexDelete(myInputInfo); break; } Console.WriteLine("--------------------------------------------------"); }
public static void RegexFind(MyInputInfo myInputInfo) { foreach (string file in myInputInfo.InputValues) { if (IsMatch(file, myInputInfo.Pattern, Multiline) == true) { Console.WriteLine($"Output[{i++}]: {Path.GetFileName(file)}"); } } }
public static void RegexDelete(MyInputInfo myInputInfo) { bool autodelete = false; foreach (string file in myInputInfo.InputValues) { if (IsMatch(file, myInputInfo.Pattern, Multiline) == false) { continue; } Console.Write($"Output[{i++}]: {Path.GetFileName(file)} OK [Y/n]: "); if (autodelete == true) { Console.WriteLine("YA"); File.Delete(file); continue; } string answer = Console.ReadLine(); if (answer == "n" || answer == "N") { continue; } if (answer == "na" || answer == "nA" || answer == "Na" || answer == "NA") { return; } if (answer == "y" || answer == "Y" || answer == "") { File.Delete(file); } if (answer == "ya" || answer == "yA" || answer == "Ya" || answer == "YA") { autodelete = true; } /*switch (Console.ReadLine()) * { * case "n": * case "N": continue; * * case "na": * case "nA": * case "Na": * case "NA": return; * * default: File.Delete(file); break; * }*/ } }