コード例 #1
0
 private void StartRemove()
 {
     try
     {
         if (options.IsSetDirectory)
         {
             string dirPath = WildcardCharacterHelper.TranslateWildcardFilePath(options.Directory);
             foreach (var pattern in options.Files)
             {
                 SearchFiles(dirPath, WildcardCharacterHelper.TranslateWildcardToRegex(pattern));
             }
         }
         else
         {
             foreach (var file in options.Files)
             {
                 string filePath = WildcardCharacterHelper.TranslateWildcardFilePath(file);
                 string dirPath  = Path.GetDirectoryName(filePath);
                 string pattern  = Path.GetFileName(filePath);
                 if (string.IsNullOrEmpty(dirPath))
                 {
                     DirectoryInfo currentDirectory = new DirectoryInfo(Environment.CurrentDirectory);
                     dirPath = currentDirectory.FullName;
                 }
                 SearchFiles(dirPath, WildcardCharacterHelper.TranslateWildcardToRegex(pattern));
             }
         }
     }
     catch (CommandLineException ex)
     {
         RaiseCommandLineException(this, ex);
     }
 }
コード例 #2
0
ファイル: SortCommandLine.cs プロジェクト: sealong/Knifer
 private void StartSort()
 {
     try
     {
         string path = WildcardCharacterHelper.TranslateWildcardFilePath(options.InputFile);
         Sort(path);
     }
     catch (CommandLineException ex)
     {
         RaiseCommandLineException(this, ex);
     }
 }
コード例 #3
0
 private void StartListDirectory()
 {
     try
     {
         string path = WildcardCharacterHelper.TranslateWildcardFilePath(options.InputDirectory);
         ListDirectory(path);
     }
     catch (CommandLineException ex)
     {
         RaiseCommandLineException(this, ex);
     }
 }
コード例 #4
0
 private void StartHead()
 {
     try
     {
         if (options.IsSetFile)
         {
             string path = WildcardCharacterHelper.TranslateWildcardFilePath(options.File);
             HeadFile(path, options.Number);
         }
     }
     catch (CommandLineException ex)
     {
         RaiseCommandLineException(this, ex);
     }
 }
コード例 #5
0
ファイル: SplitCommandLine.cs プロジェクト: softempire/Knifer
 private void StartSplit()
 {
     try
     {
         if (options.IsSetFile)
         {
             string file   = WildcardCharacterHelper.TranslateWildcardFilePath(options.File);
             string folder = WildcardCharacterHelper.TranslateWildcardDirectoryPath(options.Directory);
             SplitFile(file, folder);
         }
     }
     catch (CommandLineException ex)
     {
         RaiseCommandLineException(this, ex);
     }
 }
コード例 #6
0
 private void StartReplace()
 {
     try
     {
         if (options.IsSetInputDirectory)
         {
             string path = WildcardCharacterHelper.TranslateWildcardFilePath(options.InputDirectory);
             ReplaceDirectory(path);
         }
         else
         {
             string path = WildcardCharacterHelper.TranslateWildcardFilePath(options.InputFile);
             ReplaceFile(path);
         }
     }
     catch (CommandLineException ex)
     {
         RaiseCommandLineException(this, ex);
     }
 }
コード例 #7
0
 private void StartRemove()
 {
     try
     {
         if (options.IsSetDirectory)
         {
             string path = WildcardCharacterHelper.TranslateWildcardFilePath(options.Directory);
             SearchFiles(path);
         }
         else
         {
             foreach (var item in options.Files)
             {
                 string path = WildcardCharacterHelper.TranslateWildcardFilePath(item);
                 RemoveFile(path);
             }
         }
     }
     catch (CommandLineException ex)
     {
         RaiseCommandLineException(this, ex);
     }
 }
コード例 #8
0
        private void StartJoin()
        {
            try
            {
                string   outputFile     = WildcardCharacterHelper.TranslateWildcardFilePath(options.OutputFile);
                FileInfo outputFileInfo = new FileInfo(outputFile);
                if (outputFileInfo.Exists)
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Output file is already existent -- {0}", outputFile));
                }
                outputFileInfo.Directory.Create();

                IList <string> inputFiles = new List <string>();
                foreach (var item in options.InputFiles)
                {
                    string inputFile = WildcardCharacterHelper.TranslateWildcardFilePath(item);
                    if (!File.Exists(inputFile))
                    {
                        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                     "Input file is non-existent -- {0}", inputFile));
                    }

                    inputFiles.Add(inputFile);
                }

                DateTime beginTime = DateTime.Now;
                JoinFile(inputFiles, outputFile);
                OutputText(string.Format(CultureInfo.CurrentCulture, "Output File : {0}", outputFile));
                DateTime endTime = DateTime.Now;
                OutputText(string.Format(CultureInfo.CurrentCulture, "Total Time  : {0}s", (endTime - beginTime).TotalSeconds));
            }
            catch (CommandLineException ex)
            {
                RaiseCommandLineException(this, ex);
            }
        }
コード例 #9
0
        private void StartEncode()
        {
            try
            {
                if (options.IsSetInputFile)
                {
                    string path = WildcardCharacterHelper.TranslateWildcardFilePath(options.InputFile);
                    if (WildcardCharacterHelper.IsContainsWildcard(path))
                    {
                        FileInfo[] files = new DirectoryInfo(Path.GetDirectoryName(path)).GetFiles();
                        foreach (var file in files)
                        {
                            Regex r = new Regex(WildcardCharacterHelper.TranslateWildcardToRegex(path));
                            if (r.IsMatch(file.FullName) || r.IsMatch(file.Name))
                            {
                                EncodeFile(file.FullName);
                            }
                        }
                    }
                    else
                    {
                        EncodeFile(path);
                    }
                }

                if (options.IsSetDirectory)
                {
                    string path = WildcardCharacterHelper.TranslateWildcardDirectoryPath(options.Directory);
                    EncodeDirectory(path);
                }
            }
            catch (CommandLineException ex)
            {
                RaiseCommandLineException(this, ex);
            }
        }
コード例 #10
0
ファイル: SortCommandLine.cs プロジェクト: sealong/Knifer
        private void Sort(string path)
        {
            FileInfo file = new FileInfo(path);

            if (!file.Exists)
            {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                             "No such file -- {0}", file.FullName));
            }
            else
            {
                try
                {
                    string renamedFile = file.FullName + ".original";
                    File.Delete(renamedFile);

                    List <string> readText = new List <string>();
                    using (StreamReader sr = new StreamReader(file.FullName))
                    {
                        while (!sr.EndOfStream)
                        {
                            readText.Add(sr.ReadLine());
                        }
                    }

                    readText.Sort();
                    if (options.IsSetReverseOrder)
                    {
                        readText.Reverse();
                    }

                    using (StreamWriter sw = new StreamWriter(renamedFile, false, System.Text.Encoding.UTF8))
                    {
                        foreach (var item in readText)
                        {
                            sw.WriteLine(item);
                        }
                    }

                    if (options.IsSetOutputFile)
                    {
                        string outputPath = WildcardCharacterHelper.TranslateWildcardFilePath(options.OutputFile);
                        File.Delete(outputPath);
                        File.Move(renamedFile, outputPath);
                    }
                    else
                    {
                        File.Delete(file.FullName);
                        File.Move(renamedFile, file.FullName);
                    }
                    File.Delete(renamedFile);
                }
                catch (UnauthorizedAccessException ex)
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Operation exception -- {0}, {1}", file.FullName, ex.Message));
                }
                catch (PathTooLongException ex)
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Operation exception -- {0}, {1}", file.FullName, ex.Message));
                }
                catch (DirectoryNotFoundException ex)
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Operation exception -- {0}, {1}", file.FullName, ex.Message));
                }
                catch (NotSupportedException ex)
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Operation exception -- {0}, {1}", file.FullName, ex.Message));
                }
                catch (IOException ex)
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Operation exception -- {0}, {1}", file.FullName, ex.Message));
                }
            }
        }
コード例 #11
0
        private void AddText(string path)
        {
            FileInfo file = new FileInfo(path);

            if (!file.Exists)
            {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                             "No such file -- {0}", file.FullName));
            }
            else
            {
                try
                {
                    string text = options.Text;
                    if (options.IsSetFromFile)
                    {
                        string fromFile = WildcardCharacterHelper.TranslateWildcardFilePath(options.FromFile);
                        text = File.ReadAllText(fromFile);
                    }

                    if (options.IsSetTop)
                    {
                        string renamedFile = file.FullName + ".original";
                        File.Delete(renamedFile);
                        File.Move(file.FullName, renamedFile);

                        char[] buffer = new char[10000];

                        using (StreamReader sr = new StreamReader(renamedFile))
                            using (StreamWriter sw = new StreamWriter(file.FullName, false, System.Text.Encoding.UTF8))
                            {
                                sw.Write(text);

                                int read;
                                while ((read = sr.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    sw.Write(buffer, 0, read);
                                }
                            }

                        File.Delete(renamedFile);
                    }
                    else if (options.IsSetBottom)
                    {
                        using (StreamWriter sw = new StreamWriter(file.FullName, true, System.Text.Encoding.UTF8))
                        {
                            sw.AutoFlush = true;
                            sw.Write(text);
                        }
                    }
                }
                catch (UnauthorizedAccessException ex)
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Operation exception -- {0}, {1}", file.FullName, ex.Message));
                }
                catch (PathTooLongException ex)
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Operation exception -- {0}, {1}", file.FullName, ex.Message));
                }
                catch (DirectoryNotFoundException ex)
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Operation exception -- {0}, {1}", file.FullName, ex.Message));
                }
                catch (NotSupportedException ex)
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Operation exception -- {0}, {1}", file.FullName, ex.Message));
                }
                catch (IOException ex)
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Operation exception -- {0}, {1}", file.FullName, ex.Message));
                }
            }
        }
コード例 #12
0
        private void ReplaceFile(string path)
        {
            if (IsCanReplaceFile(path))
            {
                FileInfo file = new FileInfo(path);
                if (!file.Exists)
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "No such file -- {0}", file.FullName));
                }
                else
                {
                    try
                    {
                        string renamedFile = file.FullName + ".original";
                        File.Delete(renamedFile);

                        using (StreamReader sr = new StreamReader(file.FullName))
                            using (StreamWriter sw = new StreamWriter(renamedFile, false, System.Text.Encoding.UTF8))
                            {
                                while (!sr.EndOfStream)
                                {
                                    sw.WriteLine(sr.ReadLine().Replace(options.FromText, options.ToText));
                                }
                            }

                        if (options.IsSetOutputFile)
                        {
                            string outputPath = WildcardCharacterHelper.TranslateWildcardFilePath(options.OutputFile);
                            File.Delete(outputPath);
                            File.Move(renamedFile, outputPath);
                            OutputText(string.Format(CultureInfo.CurrentCulture, "File : {0}", outputPath));
                        }
                        else
                        {
                            File.Delete(file.FullName);
                            File.Move(renamedFile, file.FullName);
                            OutputText(string.Format(CultureInfo.CurrentCulture, "File : {0}", file.FullName));
                        }
                        File.Delete(renamedFile);
                    }
                    catch (UnauthorizedAccessException ex)
                    {
                        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                     "Operation exception -- {0}, {1}", file.FullName, ex.Message));
                    }
                    catch (PathTooLongException ex)
                    {
                        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                     "Operation exception -- {0}, {1}", file.FullName, ex.Message));
                    }
                    catch (DirectoryNotFoundException ex)
                    {
                        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                     "Operation exception -- {0}, {1}", file.FullName, ex.Message));
                    }
                    catch (NotSupportedException ex)
                    {
                        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                     "Operation exception -- {0}, {1}", file.FullName, ex.Message));
                    }
                    catch (IOException ex)
                    {
                        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                     "Operation exception -- {0}, {1}", file.FullName, ex.Message));
                    }
                }
            }
        }