using System; namespace Laj.SWEgenskaper { // Last modified: 2002-10-19 // Version: 0.42 ID:LAJGAIS44 // Compiles with Microsoft Visual C# .NET 7.0.9466 on Windows 95. // The source code is free. It is not under any kind of license. // If this code works, it was written by Lars Johansson, Göteborg. // If not, I don't know who wrote it. internal class CommandLine { struct SwitchOnlyOnce { internal bool T, D, E, ZF, ZE, C, M, L, R, A, S, H; } static SwitchOnlyOnce switchDone; static internal int Process(string[] args) { string[] files; bool ok = parseAll(args, out files); if (!ok) return 1; string location = System.IO.Directory.GetCurrentDirectory(); Backend.ProcessAll(location, files); return 0; } static bool parseAll(string[] args, out string[] files) { files = new string[0]; // Get ProcessSettings int k; for (k = 0; k < args.Length; k++) { string arg = args[k]; if (arg[0] == '-') { if (arg.Length < 2) { Console.WriteLine("Invalid switch: {0}", args[k]); return false; } char process = Char.ToUpper(arg[1]); switch (process) { case 'T': if (!parseT(arg) ) return false; break; case 'D': if (!parseD(arg) ) return false; break; case 'E': if (!parseE(arg) ) return false; break; case 'Z': if (!parseZ(arg) ) return false; break; case 'C': case 'M': case 'L': if (!parseCML(arg) ) return false; break; case 'R': case 'A': case 'S': case 'H': if (!parseRASH(arg) ) return false; break; default: Console.WriteLine("Invalid switch: {0}", args[k]); return false; } } else break; } if (k == args.Length) { Console.WriteLine("Que? No files..."); return false; } // Get files files = new string[args.Length-k]; for (int j = 0; j < args.Length-k; j++) { files[j] = args[k+j]; //Console.WriteLine("Arg {0}: {1}", j, args[k+j]); } return true; } static bool parseT(string arg) { if (switchDone.T) { Console.WriteLine("Switch already used: {0}", arg); return false; } else if (arg.Length == 2) { Backend.PS.WalkSubfolders = true; switchDone.T = true; //Console.WriteLine("T..."); } else { Console.WriteLine("Invalid switch: {0}", arg); return false; } return true; } static bool parseD(string arg) { if (switchDone.D) { Console.WriteLine("Switch already used: {0}", arg); return false; } else if (arg.Length == 2) { Backend.PS.ProcessFolders = true; switchDone.D = true; //Console.WriteLine("D..."); } else { Console.WriteLine("Invalid switch: {0}", arg); return false; } return true; } static bool parseE(string arg) { if (switchDone.E) { Console.WriteLine("Switch already used: {0}", arg); return false; } if (arg.Length >= 3) { if (arg[2] == '.') { Backend.PS.Extension.Change = true; Backend.PS.Extension.NewExtension = arg.Substring(2, arg.Length-2); switchDone.E = true; //Console.WriteLine("E... " + Backend.PS.Extension.NewExtension); } else { Console.WriteLine("The extension must start with a dot (for example: E.TXT). The switch was: {0}", arg); return false; } } else { Console.WriteLine("You must give an extension (for example: E.TXT)for the switch: {0}", arg); return false; } return true; } static bool parseZF(bool toUpper) { if (switchDone.ZF) { Console.WriteLine("Switch already used: -ZF"); return false; } else { Backend.PS.Capitalization.FileName.Change = true; Backend.PS.Capitalization.FileName.ToUpper = toUpper; switchDone.ZF = true; } return true; } static bool parseZE(bool toUpper) { if (switchDone.ZE) { Console.WriteLine("Switch already used: -ZE"); return false; } else { Backend.PS.Capitalization.Extension.Change = true; Backend.PS.Capitalization.Extension.ToUpper = toUpper; switchDone.ZE = true; } return true; } static bool parseZ(string arg) { if (arg.Length == 5) // -ZFE+ { string buf = arg.Substring(2, 2).ToUpper(); if (buf == "FE" || buf == "EF") { arg = arg.Remove(2, 2); } else { Console.WriteLine("Invalid switch: {0}", arg); return false; } } if (arg.Length == 3) // -Z+ { if (arg[2] == '+') { parseZF(true); parseZE(true); //Console.WriteLine("-Z+... "); } else if (arg[2] == '-') { parseZF(false); parseZE(false); //Console.WriteLine("-Z-... "); } else { Console.WriteLine("You indicate the Capitalization with a + or a -. The switch was: {0}", arg); return false; } } else if (arg.Length == 4) // -ZF+ or -ZE- { char fe = Char.ToUpper(arg[2]); if (fe == 'F') { if (arg[3] == '+') { parseZF(true); //Console.WriteLine("-ZF+... "); } else if (arg[3] == '-') { parseZF(false); //Console.WriteLine("-ZF-... "); } else { Console.WriteLine("You indicate the Capitalization with a + or a -. The switch was: {0}", arg); return false; } } else if (fe == 'E') { if (arg[3] == '+') { parseZF(true); parseZE(true); //Console.WriteLine("-ZE+... "); } else if (arg[3] == '-') { parseZF(false); parseZE(false); //Console.WriteLine("-ZE-... "); } else { Console.WriteLine("You indicate the Capitalization with a + or a -. The switch was: {0}", arg); return false; } } else { Console.WriteLine("You indicate File with F and Extension with E. The switch was: {0}", arg); return false; } } else { Console.WriteLine("Invalid switch: {0}", arg); return false; } return true; } static bool parseTime(string timeString, out DateTime time) { bool ok = true; time = DateTime.Now; try { time = DateTime.ParseExact(timeString, "yyyyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture); } catch (Exception exc) { Console.WriteLine(exc.Message); ok = false; } return ok; } static bool parseC(string timeString) { if (switchDone.C) { Console.WriteLine("Switch already used: -C"); return false; } Backend.PS.Touch.ChangeCreated = true; switchDone.C = true; return parseTime(timeString, out Backend.PS.Touch.NewCreationTime); } static bool parseM(string timeString) { if (switchDone.M) { Console.WriteLine("Switch already used: -M"); return false; } Backend.PS.Touch.ChangeModified = true; switchDone.M = true; return parseTime(timeString, out Backend.PS.Touch.NewLastWriteTime); } static bool parseL(string timeString) { if (switchDone.L) { Console.WriteLine("Switch already used: -L"); return false; } Backend.PS.Touch.ChangeAccessed = true; switchDone.L = true; return parseTime(timeString, out Backend.PS.Touch.NewLastAccessTime); } static bool parseCML(string arg) { if (arg.Length == 16) { switch (Char.ToUpper(arg[1]) ) { case 'C': return parseC(arg.Substring(2, 14) ); case 'L': return parseL(arg.Substring(2, 14) ); case 'M': return parseM(arg.Substring(2, 14) ); default: Console.WriteLine("Invalid switch: {0}", arg); return false; } } else if (arg.Length == 17) { bool ok; string buf = arg.Substring(1, 2).ToUpper(); switch (buf) { case "CL": case "LC": ok = parseC(arg.Substring(3, 14) ); ok &= parseL(arg.Substring(3, 14) ); return ok; case "CM": case "MC": ok = parseC(arg.Substring(3, 14) ); ok &= parseM(arg.Substring(3, 14) ); return ok; case "LM": case "ML": ok = parseL(arg.Substring(3, 14) ); ok &= parseM(arg.Substring(3, 14) ); return ok; default: Console.WriteLine("Invalid switch: {0}", arg); return false; } } if (arg.Length == 18) { bool ok; string buf = arg.Substring(1, 3).ToUpper(); switch (buf) { case "CLM": case "CML": case "LCM": case "LMC": case "MCL": case "MLC": ok = parseC(arg.Substring(4, 14) ); ok &= parseL(arg.Substring(4, 14) ); ok &= parseM(arg.Substring(4, 14) ); return ok; default: Console.WriteLine("Invalid switch: {0}", arg); return false; } } else { Console.WriteLine("You must give a date on the form yyyyMMddHHmmss for the switch: {0}", arg); return false; } } static bool parseR(bool b) { if (switchDone.R) { Console.WriteLine("Switch already used: -R"); return false; } Backend.PS.Attributes.ChangeReadOnly = true; Backend.PS.Attributes.ReadOnly = b; switchDone.R = true; return true; } static bool parseA(bool b) { if (switchDone.A) { Console.WriteLine("Switch already used: -A"); return false; } Backend.PS.Attributes.ChangeArchive = true; Backend.PS.Attributes.Archive = b; switchDone.A = true; return true; } static bool parseS(bool b) { if (switchDone.S) { Console.WriteLine("Switch already used: -S"); return false; } Backend.PS.Attributes.ChangeSystem = true; Backend.PS.Attributes.System = b; switchDone.S = true; return true; } static bool parseH(bool b) { if (switchDone.H) { Console.WriteLine("Switch already used: -H"); return false; } Backend.PS.Attributes.ChangeHidden = true; Backend.PS.Attributes.Hidden = b; switchDone.H = true; return true; } static bool parseRASH(string arg) { if (arg.Length >= 3 && arg.Length <= 6) { string buf = arg.Substring(1); char op = arg[arg.Length-1]; bool setAttr; if (op == '+') setAttr = true; else if (op == '-') setAttr = false; else { Console.WriteLine("+ Sets an attribute; - Clears an attribute. The switch was: {0}", arg); return false; } for (int k = 1; k <= arg.Length-2; k++) { bool ok; switch (Char.ToUpper(arg[k]) ) { case 'R': ok = parseR(setAttr); break; case 'A': ok = parseA(setAttr); break; case 'S': ok = parseS(setAttr); break; case 'H': ok = parseH(setAttr); break; default: Console.WriteLine("Invalid switch: {0}", arg); return false; } if (!ok) return false; } } else { Console.WriteLine("Invalid switch: {0}", arg); return false; } return true; } static internal int WriteHelp() { Console.WriteLine("\nChanges file properties"); Console.Write("\nSWEgenskaper "); Console.WriteLine("[-T] [-D] [-E] [-Z] [-C] [-M] [-L] [-R] [-A] [-S] [-H]"); Console.WriteLine("[ [drive:] [path] filename] [+ [drive:] [path] filename] [+ ...]"); Console.WriteLine("\nFolder options:"); Console.WriteLine(" -T Processes matching files in the current folder and all subfolders."); Console.WriteLine(" -D Processes folders as well."); Console.WriteLine("\nExtension:"); Console.WriteLine(" -E"); Console.WriteLine(" Example:"); Console.WriteLine(" -E.txt changes the Extension to .txt"); Console.WriteLine(" -E.DOC changes the Extension to .DOC"); Console.WriteLine("\nCapitalization:"); Console.WriteLine(" -Z"); Console.WriteLine(" Options:"); Console.WriteLine(" + = Upper case; - Lower case"); Console.WriteLine(" F = File name; E Extension"); Console.WriteLine(" Example:"); Console.WriteLine(" -Z+ changes the File name and the Extension to Upper case."); Console.WriteLine(" -ZE- changes the Extension to Lower case."); Console.WriteLine("\nTime"); Console.WriteLine(" -C Created"); Console.WriteLine(" -M Modified"); Console.WriteLine(" -L Last accessed"); Console.WriteLine(" Time format:"); Console.WriteLine(" YYMMDDhhmmss"); Console.WriteLine(" Example:"); Console.WriteLine(" -C19991112144500 changes the Created time to 1999-11-12 14:45:00"); Console.WriteLine(" -CM19991112144500 changes both Created and Modified time."); Console.WriteLine("\nAttributes:"); Console.WriteLine(" -R Read-only file attribute."); Console.WriteLine(" -A Archive file attribute."); Console.WriteLine(" -S System file attribute."); Console.WriteLine(" -H Hidden file attribute."); Console.WriteLine(" Options:"); Console.WriteLine(" + = Set; - Reset"); Console.WriteLine(" Example:"); Console.WriteLine(" -A+ sets the Archive file attribute."); Console.WriteLine(" -AR- resets the Archive file attribute and the Read-only file attribute."); return 0; } private CommandLine() { } } }