コード例 #1
0
        /// <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));
                    }
                }
            }
        }
コード例 #2
0
ファイル: ddupe.cs プロジェクト: jwg4/gtools
        /// <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));
            }
        }
コード例 #3
0
        /// <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));
                    }
                }
            }
        }
コード例 #4
0
        /// <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);
                    }
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// Locate all instances of the given filename
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        private IEnumerable <string> Locate(string filename)
        {
            string originalFilename = filename;

            // if the user is looking for a special
            List <string> ExtensionsToFind = Args.GetStringList("extension");
            string        e = GetExtension(filename);

            if (e.Length > 0)
            {
                ExtensionsToFind = new List <string>();;
                ExtensionsToFind.Add(e);
                int k = filename.LastIndexOf('.');
                filename = filename.Substring(0, k);
            }

            string lookfor = filename;

            if (!lookfor.EndsWith("*"))
            {
                lookfor += "*";
            }

            SearchOption so = SearchOption.TopDirectoryOnly;

            if (Args.GetFlag("recursive"))
            {
                so = SearchOption.AllDirectories;
            }

            foreach (string directory in Args.GetStringList("dir"))
            {
                string[] files;
                if (string.IsNullOrEmpty(directory))
                {
                    continue;
                }
                else if (!Directory.Exists(directory))
                {
                    Console.WriteLine(resource.IDS_ERR_dir_does_not_exist, directory);
                    continue;
                }
                try
                {
                    files = Directory.GetFiles(directory, lookfor, so);
                }
                catch (ArgumentException ep)
                {
                    Console.WriteLine(ep);
                    Console.WriteLine(ep.StackTrace);
                    Console.WriteLine(resource.IDS_ERR_while_parsing, directory);
                    Console.WriteLine(resource.IDS_ERR_looking_for, lookfor);
                    break;
                }
                catch (Exception ep)
                {
                    Console.WriteLine(ep);
                    Console.WriteLine(ep.StackTrace);
                    Console.WriteLine(resource.IDS_ERR_while_parsing, directory);
                    continue;
                }
                bool   hasWildcard   = HasWildcard(originalFilename);
                string lowerFilename = originalFilename.ToLower();

                foreach (string foundname in files)
                {
                    if (!hasWildcard && !foundname.ToLower().Contains(lowerFilename))
                    {
                        continue;
                    }
                    e = GetExtension(foundname);

                    if (Contains(ExtensionsToFind, e))
                    {
                        yield return(foundname);
                    }
                }
            }
        }
コード例 #6
0
ファイル: pathed.cs プロジェクト: jwg4/gtools
        /// <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();
            }
        }