/// <summary> /// This program calculates the MD5 hashes for the given input files /// </summary> /// <param name="args"></param> private void Run(string[] args) { Console.OutputEncoding = Encoding.GetEncoding(Encoding.Default.CodePage); Args = new InputArgs("md5sums", string.Format(resource.IDS_TITLE, AppVersion.Get()) + "\r\n" + resource.IDS_COPYRIGHT); Args.Add(InputArgType.Flag, "recursive", false, Presence.Optional, resource.IDS_CMD_recursive_doc); Args.Add(InputArgType.RemainingParameters, "DIR {DIR}", null, Presence.Optional, resource.IDS_CMD_dir_doc); if (Args.Process(args)) { Recursive = Args.GetFlag("recursive"); List<string> directories = Args.GetStringList("DIR {DIR}"); if (directories == null) { Read(Directory.GetCurrentDirectory()); } else { foreach (string directory in directories) { Read(SanitizeInput(directory)); } } } }
/// <summary> /// This program allows you to view and modify the PATH environment. /// </summary> /// <param name="args"></param> private void Run(string[] args) { Console.OutputEncoding = Encoding.GetEncoding(Encoding.Default.CodePage); Args = new InputArgs("pathed", string.Format(resource.IDS_TITLE, AppVersion.Get()) + "\r\n" + resource.IDS_COPYRIGHT); Args.Add(InputArgType.Flag, "machine", false, Presence.Optional, resource.IDS_CMD_machine_doc); Args.Add(InputArgType.Flag, "user", false, Presence.Optional, resource.IDS_CMD_user_doc); Args.Add(InputArgType.ExistingDirectory, "add", "", Presence.Optional, resource.IDS_CMD_add_doc); Args.Add(InputArgType.ExistingDirectory, "append", "", Presence.Optional, resource.IDS_CMD_append_doc); Args.Add(InputArgType.StringList, "remove", null, Presence.Optional, resource.IDS_CMD_remove_doc); Args.Add(InputArgType.Flag, "slim", false, Presence.Optional, resource.IDS_CMD_slim_doc); Args.Add(InputArgType.Parameter, "env", "PATH", Presence.Optional, resource.IDS_CMD_env_doc); if (Args.Process(args)) { EnvironmentVariableName = Args.GetString("env"); if (Args.GetFlag("slim")) SlimPath(); if (Args.GetFlag("machine")) EnvironmentVariableTarget = EnvironmentVariableTarget.Machine; else if (Args.GetFlag("user")) EnvironmentVariableTarget = EnvironmentVariableTarget.User; try { List<string> removeItems = Args.GetStringList("remove"); if (removeItems != null) Remove(removeItems); string add = Args.GetString("add"); if (!string.IsNullOrEmpty(add)) AddHead(SanitizePath(add)); string append = Args.GetString("append"); if (!string.IsNullOrEmpty(append)) AddTail(SanitizePath(append)); } catch (SecurityException ex) { if (EnvironmentVariableTarget == EnvironmentVariableTarget.Machine) { Console.WriteLine(ex.Message); Console.WriteLine(resource.IDS_ERR_access_denied); return; } else throw; } ListPath(); } }
/// <summary> /// This program finds an executable on the PATH. It can also find other stuff on the path, but /// mostly it finds the executable.s /// </summary> /// <param name="args"></param> private void Run(string[] args) { Console.OutputEncoding = Encoding.GetEncoding(Encoding.Default.CodePage); Args = new InputArgs("su", string.Format(resource.IDS_TITLE, AppVersion.Get()) + "\r\n" + resource.IDS_COPYRIGHT); Args.Add(InputArgType.Parameter, "cmd", "cmd.exe", Presence.Optional, resource.IDS_DOC_cmd_param); if (Args.Process(args)) { string command = Args.GetString("cmd"); if( command.Equals("cmd.exe", StringComparison.OrdinalIgnoreCase)) { if (!IsAdministrator()) { ProcessStartInfo startInfo = new ProcessStartInfo(Path.Combine(Environment.SystemDirectory, "cmd.exe")); startInfo.Verb = "runas"; startInfo.WorkingDirectory = Environment.CurrentDirectory; startInfo.Arguments = string.Format("/K \"cd /d {0}\"", startInfo.WorkingDirectory); System.Diagnostics.Process.Start(startInfo); } else { Console.WriteLine(resource.IDS_ERR_AlreadyRunningAsAdministrator); } } else { string exe; if( ProcessInfoTools.FindExecutable(command, out exe) ) { ProcessStartInfo startInfo = new ProcessStartInfo(exe); startInfo.Verb = "runas"; startInfo.WorkingDirectory = Environment.CurrentDirectory; System.Diagnostics.Process.Start(startInfo); } else { Console.WriteLine(resource.IDS_ERR_UnableToFindFile, command); } } } }
/// <summary> /// This program calculates the MD5 hashes for the given input files /// </summary> /// <param name="args"></param> private void Run(string[] args) { Console.OutputEncoding = Encoding.GetEncoding(Encoding.Default.CodePage); Args = new InputArgs( "touch", string.Format("Version {0}\r\nFreeware written by Gerson Kurz (http://p-nand-q.com)", AppVersion.Get())); Args.Add(InputArgType.Flag, "recursive", false, Presence.Optional, "search directories recursively"); Args.Add(InputArgType.RemainingParameters, "DIR {DIR}", null, Presence.Optional, "one or more directories to search"); if (Args.Process(args)) { Recursive = Args.GetFlag("recursive"); List<string> directories = Args.GetStringList("DIR {DIR}"); if (directories == null) { Read(Directory.GetCurrentDirectory()); } else { foreach (string directory in directories) { Read(SanitizeInput(directory)); } } } }
/// <summary> /// This program finds an executable on the PATH. It can also find other stuff on the path, but /// mostly it finds the executable.s /// </summary> /// <param name="args"></param> private void Run(string[] args) { Console.OutputEncoding = Encoding.GetEncoding(Encoding.Default.CodePage); Args = new InputArgs( "DETECTDUPLICATES", string.Format("Version {0}\r\nFreeware written by Gerson Kurz (http://p-nand-q.com)", AppVersion.Get())); Args.Add(InputArgType.Flag, "recursive", false, Presence.Optional, "search directories recursively"); Args.Add(InputArgType.Parameter, "cache", null, Presence.Optional, "cache file name"); Args.Add(InputArgType.Flag, "delete", false, Presence.Optional, "delete files (default: list only)"); Args.Add(InputArgType.RemainingParameters, "DIR {DIR}", null, Presence.Required, "one or more directories to search"); if (Args.Process(args)) { DatabaseCache.Initialize(Args.GetString("cache"), Cache); Recursive = Args.GetFlag("recursive"); DeleteFiles = Args.GetFlag("delete"); foreach (string directory in Args.GetStringList("DIR {DIR}")) { Read(SanitizeInput(directory)); } } if (FilesChecked > 0) { DatabaseCache.Flush(); TimeSpan elapsed = DateTime.Now - StartupTime; Console.WriteLine("____________________________________________________________________________________"); Console.WriteLine("DetectDuplicates finished after {0}", elapsed); Console.WriteLine("Checked a total of {0} files using {1}, calculating {2} hashes.", FilesChecked, Tools.BytesAsString(BytesChecked), HashesRead); Console.WriteLine("Of these, {0} files [{2:0.00%}] using {1} [{3:0.00%}] were duplicates.", FilesDetected, Tools.BytesAsString(BytesDetected), ((double)FilesDetected) / ((double)FilesChecked), ((double)BytesDetected) / ((double)BytesChecked)); } }
/// <summary> /// This program allows you to view and modify the PATH environment. /// </summary> /// <param name="args"></param> private void Run(string[] args) { Console.OutputEncoding = Encoding.GetEncoding(Encoding.Default.CodePage); Args = new InputArgs( "PATHED", string.Format( "Version {0}\r\nFreeware written by Gerson Kurz (http://p-nand-q.com)", AppVersion.Get())); Args.Add(InputArgType.Flag, "machine", false, Presence.Optional, "print machine PATH"); Args.Add(InputArgType.Flag, "user", false, Presence.Optional, "print user PATH"); Args.Add(InputArgType.ExistingDirectory, "add", "", Presence.Optional, "add variable at the head"); Args.Add(InputArgType.ExistingDirectory, "append", "", Presence.Optional, "add variable at the tail"); Args.Add(InputArgType.StringList, "remove", null, Presence.Optional, "remove path / index"); Args.Add(InputArgType.Flag, "slim", false, Presence.Optional, "strip duplicate vars"); Args.Add(InputArgType.Parameter, "env", "PATH", Presence.Optional, "environment variable, defaults to PATH"); if (Args.Process(ref args)) { EnvironmentVariableName = Args.GetString("env"); if (Args.GetFlag("slim")) SlimPath(); if (Args.GetFlag("machine")) EnvironmentVariableTarget = EnvironmentVariableTarget.Machine; else if (Args.GetFlag("user")) EnvironmentVariableTarget = EnvironmentVariableTarget.User; try { List<string> removeItems = Args.GetStringList("remove"); if (removeItems != null) Remove(removeItems); string add = Args.GetString("add"); if (!string.IsNullOrEmpty(add)) AddHead(add); string append = Args.GetString("append"); if (!string.IsNullOrEmpty(append)) AddTail(append); } catch (SecurityException ex) { if (EnvironmentVariableTarget == EnvironmentVariableTarget.Machine) Console.WriteLine("ERROR, cannot manipulate the machine environment variables. {0}", ex.Message); else throw; } ListPath(); } }
/// <summary> /// This program finds an executable on the PATH. It can also find other stuff on the path, but /// mostly it finds the executable.s /// </summary> /// <param name="args"></param> private void Run(string[] args) { Console.OutputEncoding = Encoding.GetEncoding(Encoding.Default.CodePage); Args = new InputArgs( "which", string.Format( resource.IDS_TITLE, AppVersion.Get() ) + "\r\n" + resource.IDS_COPYRIGHT ); Args.Add(InputArgType.StringList, "extension", null, Presence.Optional, resource.IDS_CMD_extension_doc); Args.Add(InputArgType.StringList, "dir", null, Presence.Optional, resource.IDS_CMD_dir_doc); Args.Add(InputArgType.Flag, "recursive", false, Presence.Optional, resource.IDS_CMD_recursive_doc); Args.Add(InputArgType.Flag, "single", false, Presence.Optional, resource.IDS_CMD_single_doc); Args.Add(InputArgType.RemainingParameters, "FILE {FILE}", null, Presence.Required, resource.IDS_CMD_file_doc); Args.Add(InputArgType.Parameter, "env", "PATH", Presence.Optional, resource.IDS_CMD_env_doc); if (Args.Process(args)) { Filenames = Args.GetStringList("FILE {FILE}"); Directories = Args.FindOrCreateStringList("dir"); string EnvironmentVariableName = Args.GetString("env"); if (!string.IsNullOrEmpty(EnvironmentVariableName)) { string env = Environment.GetEnvironmentVariable(EnvironmentVariableName, EnvironmentVariableTarget.User); if( string.IsNullOrEmpty(env) ) { env = Environment.GetEnvironmentVariable(EnvironmentVariableName); } EnvironmentVariablesAlreadyChecked[EnvironmentVariableName] = true; if( string.IsNullOrEmpty(env) ) { Console.WriteLine(resource.IDS_ERR_invalid_env_var, EnvironmentVariableName); } else { foreach (string token in env.Split(';')) { Directories.Add(token); } } } if( FilenamesAreIncludes() ) { AddEnvBasedDirectories("INCLUDE"); } else if (FilenamesAreLibs()) { AddEnvBasedDirectories("LIB"); } else { // default: use standard windows lookup Directories.Add( Directory.GetCurrentDirectory()); Directories.Add(PathSanitizer.GetWindowsDirectory()); Directories.Add(PathSanitizer.Get32BitSystemDirectory()); Directories.Add(Environment.SystemDirectory); Directories.Add(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)); Directories.Add( Environment.GetFolderPath(Environment.SpecialFolder.System)); Directories.Add( Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.System))); AddEnvBasedDirectories("PATH"); } Directories.MakeUnique(StringComparison.OrdinalIgnoreCase); List<string> Extensions = Args.FindOrCreateStringList("extension"); if (Extensions.Count == 0) { foreach (string path in Environment.GetEnvironmentVariable("PATHEXT").Split(';')) { Extensions.Add(path); } } List<string> FoundItems = new List<string>(); foreach (string filename in Filenames) { bool found = false; foreach (string foundname in Locate(filename)) { if (!Contains(FoundItems, foundname)) { FileInfo fi = new FileInfo(foundname); Console.WriteLine(resource.IDS_RESULT_PATTERN, foundname, fi.LastWriteTime, fi.Length); FoundItems.Add(foundname); if (Args.GetFlag("single")) break; found = true; } } if( !found ) { Console.WriteLine(resource.IDS_ERR_not_found, filename); } } } }
/// <summary> /// This program finds an executable on the PATH. It can also find other stuff on the path, but /// mostly it finds the executable.s /// </summary> /// <param name="args"></param> private void Run(string[] args) { Console.OutputEncoding = Encoding.GetEncoding(Encoding.Default.CodePage); Args = new InputArgs( "WHICH", string.Format( "Version {0}\r\nFreeware written by Gerson Kurz (http://p-nand-q.com)", AppVersion.Get())); Args.Add(InputArgType.StringList, "extension", null, Presence.Optional, "search for extension <name>, can be a ; separated list"); Args.Add(InputArgType.StringList, "dir", null, Presence.Optional, "add directory <name>, can be a ; separated list"); Args.Add(InputArgType.Flag, "recursive", false, Presence.Optional, "search directories recursively"); Args.Add(InputArgType.Flag, "single", false, Presence.Optional, "stop after the first find result"); Args.Add(InputArgType.RemainingParameters, "FILE {FILE}", null, Presence.Required, "one or more files to find"); Args.Add(InputArgType.Parameter, "env", "PATH", Presence.Optional, "environment variable, defaults to PATH"); if (Args.Process(args)) { Filenames = Args.GetStringList("FILE {FILE}"); Directories = Args.FindOrCreateStringList("dir"); string EnvironmentVariableName = Args.GetString("env"); if( EnvironmentVariableName != null ) { foreach (string token in Environment.GetEnvironmentVariable(EnvironmentVariableName, EnvironmentVariableTarget.User).Split(';')) { Directories.Add(token); } } if( FilenamesAreIncludes() ) { AddEnvBasedDirectories("INCLUDE"); } else if (FilenamesAreLibs()) { AddEnvBasedDirectories("LIB"); } else { // default: use standard windows lookup Directories.Add( Directory.GetCurrentDirectory()); Directories.Add( Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)); Directories.Add( Environment.GetFolderPath(Environment.SpecialFolder.System)); Directories.Add( Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.System))); AddEnvBasedDirectories("PATH"); } StringList.MakeUnique(ref Directories, StringComparison.OrdinalIgnoreCase); List<string> Extensions = Args.FindOrCreateStringList("extension"); if (Extensions.Count == 0) { foreach (string path in Environment.GetEnvironmentVariable("PATHEXT").Split(';')) { Extensions.Add(path); } } List<string> FoundItems = new List<string>(); foreach (string filename in Filenames ) { foreach (string foundname in Locate(filename)) { if (!Contains(FoundItems, foundname)) { FileInfo fi = new FileInfo(foundname); Console.WriteLine("{0} [{1}, {2} bytes]", foundname, fi.LastWriteTime, fi.Length); FoundItems.Add(foundname); if (Args.GetFlag("single")) break; } } } } }
/// <summary> /// This program calculates the MD5 hashes for the given input files /// </summary> /// <param name="args"></param> private void Run(string[] args) { Console.OutputEncoding = Encoding.GetEncoding(Encoding.Default.CodePage); Args = new InputArgs("touch", string.Format(resource.IDS_TITLE, AppVersion.Get()) + "\r\n" + resource.IDS_COPYRIGHT); Args.Add(InputArgType.Parameter, "date", "", Presence.Optional, resource.IDS_CMD_date_doc); Args.Add(InputArgType.Parameter, "time", "", Presence.Optional, resource.IDS_CMD_time_doc); Args.Add(InputArgType.Flag, "recursive", false, Presence.Optional, resource.IDS_CMD_recursive_doc); Args.Add(InputArgType.RemainingParameters, "DIR {DIR}", null, Presence.Optional, resource.IDS_CMD_dir_doc); if (Args.Process(args)) { DateTime dateSpec = DateTime.Now; DateTime timeSpec = DateTime.Now; string date = Args.GetString("DATE"); if (!string.IsNullOrEmpty(date)) { try { dateSpec = DateTime.Parse(date); } catch(Exception e) { Console.WriteLine(e); Console.WriteLine(e.StackTrace); Console.WriteLine(resource.IDS_ERR_unable_to_decode_date, date); return; } } string time = Args.GetString("TIME"); if (!string.IsNullOrEmpty(time)) { try { timeSpec = DateTime.Parse(time); } catch (Exception e) { Console.WriteLine(e); Console.WriteLine(e.StackTrace); Console.WriteLine(resource.IDS_ERR_unable_to_decode_time, time); return; } } TimeStamp = new DateTime(dateSpec.Year, dateSpec.Month, dateSpec.Day, timeSpec.Hour, timeSpec.Minute, timeSpec.Second); Recursive = Args.GetFlag("recursive"); List<string> directories = Args.GetStringList("DIR {DIR}"); if (directories == null) { Read(Directory.GetCurrentDirectory()); } else { foreach (string directory in directories) { Read(SanitizeInput(directory)); } } } }
/// <summary> /// This program finds an executable on the PATH. It can also find other stuff on the path, but /// mostly it finds the executable.s /// </summary> /// <param name="args"></param> private void Run(string[] args) { Console.OutputEncoding = Encoding.GetEncoding(Encoding.Default.CodePage); Args = new InputArgs("ddupe", string.Format(resource.IDS_TITLE, AppVersion.Get()) + "\r\n" + resource.IDS_COPYRIGHT); Args.Add(InputArgType.Flag, "recursive", false, Presence.Optional, resource.IDS_CMD_recursive_doc); Args.Add(InputArgType.Flag, "rename", false, Presence.Optional, resource.IDS_CMD_rename_doc); Args.Add(InputArgType.Parameter, "cache", null, Presence.Optional, resource.IDS_CMD_cache_doc); Args.Add(InputArgType.Flag, "delete", false, Presence.Optional, resource.IDS_CMD_delete_doc); Args.Add(InputArgType.SizeInBytes, "minsize", 0, Presence.Optional, resource.IDS_CMD_minsize_doc); Args.Add(InputArgType.RemainingParameters, "DIR {DIR}", null, Presence.Required, resource.IDS_CMD_dir_doc); if (Args.Process(args)) { UseDatabase = DatabaseCache.Initialize(Args.GetString("cache"), Cache); Recursive = Args.GetFlag("recursive"); RenameFiles = Args.GetFlag("rename"); DeleteFiles = Args.GetFlag("delete"); MinSize = Args.GetSizeInBytes("minsize"); foreach (string directory in Args.GetStringList("DIR {DIR}")) { Read(SanitizeInput(directory)); } } if (FilesChecked > 0) { DatabaseCache.Flush(); TimeSpan elapsed = DateTime.Now - StartupTime; Console.WriteLine("____________________________________________________________________________________"); Console.WriteLine(resource.IDS_finished, elapsed); Console.WriteLine(resource.IDS_total_checked, FilesChecked, BytesAsString(BytesChecked), HashesRead, TimeSpentCalculatingMD5s); if (RenameFiles) { Console.WriteLine(resource.IDS_total_renamed, FilesRenamed, BytesAsString(BytesRenamed)); Console.WriteLine(resource.IDS_total_hashes, RenamedHashes, BytesAsString(RenamedBytes)); } Console.WriteLine(resource.IDS_total_dupes, FilesDetected, BytesAsString(BytesDetected), ((double)FilesDetected) / ((double)FilesChecked), ((double)BytesDetected) / ((double)BytesChecked)); } }