private static void CheckOptions(EncodeCommandLineOptions checkedOptions) { if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion) { if (!checkedOptions.IsSetInputFile && !checkedOptions.IsSetDirectory) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a input file or a directory.")); } if (!checkedOptions.IsSetFromEncoding) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify the input file current encoding.")); } if (!checkedOptions.IsSetToEncoding) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify the output file target encoding.")); } if (checkedOptions.IsSetInputFile && WildcardCharacterHelper.IsContainsWildcard(checkedOptions.InputFile) && checkedOptions.IsSetOutputFile) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "output file path has been set, so can only set one input file.")); } if (checkedOptions.IsSetDirectory && checkedOptions.IsSetOutputFile) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "output file path has been set, so can not set a input directory.")); } } }
private void MoveFile(string path) { FileInfo file = new FileInfo(path); if (!file.Exists) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "No such file -- {0}", file.FullName)); } else { Regex r = new Regex(WildcardCharacterHelper.TranslateWildcardToRegex(options.RegexPattern), RegexOptions.IgnoreCase); Match m = r.Match(file.Name); if (m.Success) { string folderName = file.LastWriteTime.ToString(@"yyyyMMdd"); string folderPath = Path.Combine(file.Directory.FullName, folderName); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } string newPath = Path.Combine(folderPath, file.Name); file.MoveTo(newPath); OutputText(string.Format(CultureInfo.CurrentCulture, "File From: {0}", path)); OutputText(string.Format(CultureInfo.CurrentCulture, " To : {0}", newPath)); } } }
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); } }
private void FindFile(string directoryName, string fileName) { Regex r = new Regex(WildcardCharacterHelper.TranslateWildcardToRegex(options.RegexPattern)); if (r.IsMatch(fileName)) { OutputText(Path.Combine(directoryName, fileName)); } }
private void StartRemoveDirectory() { try { string path = WildcardCharacterHelper.TranslateWildcardDirectoryPath(options.Directory); SearchDirectory(path); } catch (CommandLineException ex) { RaiseCommandLineException(this, ex); } }
private void StartListDirectory() { try { string path = WildcardCharacterHelper.TranslateWildcardFilePath(options.InputDirectory); ListDirectory(path); } catch (CommandLineException ex) { RaiseCommandLineException(this, ex); } }
private void StartSort() { try { string path = WildcardCharacterHelper.TranslateWildcardFilePath(options.InputFile); Sort(path); } catch (CommandLineException ex) { RaiseCommandLineException(this, ex); } }
private void StartSyncCopy() { try { string fromDirectory = WildcardCharacterHelper.TranslateWildcardDirectoryPath(options.FromDirectory); string toDirectory = WildcardCharacterHelper.TranslateWildcardDirectoryPath(options.ToDirectory); SyncCopy(fromDirectory, toDirectory); } catch (CommandLineException ex) { RaiseCommandLineException(this, ex); } }
private void StartSelect() { try { if (options.IsSetDirectory) { string path = WildcardCharacterHelper.TranslateWildcardDirectoryPath(options.Directory); SelectDirectory(path); } } catch (CommandLineException ex) { RaiseCommandLineException(this, ex); } }
private void StartHead() { try { if (options.IsSetFile) { string path = WildcardCharacterHelper.TranslateWildcardFilePath(options.File); HeadFile(path, options.Number); } } catch (CommandLineException ex) { RaiseCommandLineException(this, ex); } }
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); } }
private void StartCopyFiles() { try { if (options.IsSetSourceFolder && options.IsSetDestinationFolder) { string sourceFolder = WildcardCharacterHelper.TranslateWildcardDirectoryPath(options.SourceFolder); string destinationFolder = WildcardCharacterHelper.TranslateWildcardDirectoryPath(options.DestinationFolder); CopySameFiles(sourceFolder, destinationFolder); } } catch (CommandLineException ex) { RaiseCommandLineException(this, ex); } }
private void StartGrep() { try { foreach (var item in options.FilePaths) { string path = WildcardCharacterHelper.TranslateWildcardDirectoryPath(item); if (options.IsSetDirectory) { GrepDirectory(path); } else { if (WildcardCharacterHelper.IsContainsWildcard(path)) { string dir = path; if (!path.Contains("\\") && !path.Contains("/")) { dir = Environment.CurrentDirectory + Path.DirectorySeparatorChar + path; } FileInfo[] files = new DirectoryInfo(Path.GetDirectoryName(dir)).GetFiles(); foreach (var file in files.OrderBy(f => f.LastWriteTime).ThenBy(f => f.Name)) { Regex r = new Regex(WildcardCharacterHelper.TranslateWildcardToRegex(path)); if (r.IsMatch(file.FullName) || r.IsMatch(file.Name)) { GrepFile(file.FullName); } } } else { GrepFile(path); } } } } catch (ArgumentException ex) { RaiseCommandLineException(this, new CommandLineException("Path is invalid.", ex)); } catch (CommandLineException ex) { RaiseCommandLineException(this, ex); } }
private void StartRename() { try { string path = WildcardCharacterHelper.TranslateWildcardDirectoryPath(options.InputDirectory); if (options.IsSetPadString) { SearchDirectoryForPadString(path); } else { SearchDirectory(path); } } catch (CommandLineException ex) { RaiseCommandLineException(this, ex); } }
private void RenameFile(string path) { FileInfo file = new FileInfo(path); if (!file.Exists) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "No such file -- {0}", file.FullName)); } else { Regex r = new Regex(WildcardCharacterHelper.TranslateWildcardToRegex(options.RegexPattern), RegexOptions.IgnoreCase); Match m = r.Match(file.Name); if (m.Success) { string newName = ""; if (string.IsNullOrWhiteSpace(options.Prefix)) { newName = string.Format(CultureInfo.CurrentCulture, "{0}-{1}{2}", file.LastWriteTime.ToString(@"yyyyMMddHHmmss"), Guid.NewGuid(), file.Extension.ToLowerInvariant()); } else { newName = string.Format(CultureInfo.CurrentCulture, "{0}-{1}-{2}{3}", options.Prefix, file.LastWriteTime.ToString(@"yyyyMMddHHmmss"), Guid.NewGuid(), file.Extension.ToLowerInvariant()); } string newPath = Path.Combine(file.Directory.FullName, newName); file.MoveTo(newPath); OutputText(string.Format(CultureInfo.CurrentCulture, "File From: {0}", path)); OutputText(string.Format(CultureInfo.CurrentCulture, " To : {0}", newPath)); } } }
private void StartCount() { try { if (options.IsSetDirectory) { string path = WildcardCharacterHelper.TranslateWildcardDirectoryPath(options.Directory); CountDirectory(path); } foreach (var item in countSummary.OrderByDescending(t => t.Value).ThenBy(w => w.Key)) { OutputText(string.Format(CultureInfo.CurrentCulture, "FileType: {0,-30}Count: {1}", item.Key.ToLowerInvariant(), item.Value)); } } catch (CommandLineException ex) { RaiseCommandLineException(this, ex); } }
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); } }
private bool IsCanGrepFile(string file) { bool result = false; if (string.IsNullOrEmpty(file)) { result = false; } else if (executingFile == file) { result = false; } else if (file.ToUpperInvariant().EndsWith(".EXE", StringComparison.CurrentCulture)) { result = false; } else if (options.IsSetIncludeFiles) { Regex r = new Regex(WildcardCharacterHelper.TranslateWildcardToRegex(options.IncludeFilesPattern)); if (r.IsMatch(file)) { result = true; } } else if (options.IsSetExcludeFiles) { Regex r = new Regex(WildcardCharacterHelper.TranslateWildcardToRegex(options.ExcludeFilesPattern)); if (!r.IsMatch(file)) { result = true; } } else { result = true; } return(result); }
private void StartExtract() { try { DateTime executeBeginTime = DateTime.Now; string path = WildcardCharacterHelper.TranslateWildcardDirectoryPath(options.InputDirectory); ExtractDirectory(path); DateTime executeEndTime = DateTime.Now; TimeSpan duration = executeEndTime - executeBeginTime; OutputText(Environment.NewLine); OutputText(string.Format(CultureInfo.CurrentCulture, "Extract Begin Time : {0}", executeBeginTime.ToString(@"yyyy-MM-dd HH:mm:ss"))); OutputText(string.Format(CultureInfo.CurrentCulture, "Extract End Time : {0}", executeEndTime.ToString(@"yyyy-MM-dd HH:mm:ss"))); OutputText(string.Format(CultureInfo.CurrentCulture, "Extract Total Time : {0}", string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", duration.Hours, duration.Minutes, duration.Seconds, duration.Milliseconds))); } catch (CommandLineException ex) { RaiseCommandLineException(this, ex); } }
private bool IsCanGrepDirectory(string directory) { bool result = false; if (string.IsNullOrEmpty(directory)) { result = false; } else if (options.IsSetExcludeDirectories) { Regex r = new Regex(WildcardCharacterHelper.TranslateWildcardToRegex(options.ExcludeDirectoriesPattern)); if (!r.IsMatch(directory)) { result = true; } } else { result = true; } return(result); }
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); } }
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); } }
private string GetDepthOutputDirectory(string outputDirectory, string fileDirectoryName) { string outputPath = outputDirectory; if (options.IsSetKeepDepth) { string selectPath = WildcardCharacterHelper.TranslateWildcardDirectoryPath(options.Directory); string partDir = fileDirectoryName.Replace(selectPath, "") .TrimStart(new char[] { '/', '\\' }) .TrimEnd(new char[] { '/', '\\' }) .Replace('/', '|') .Replace('\\', '|'); if (!string.IsNullOrEmpty(partDir)) { string[] depthDirList = partDir.Split('|').Take(options.KeepDepth).ToArray(); foreach (var item in depthDirList) { outputPath = Path.Combine(outputPath, item); } } } return(outputPath); }
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); } }
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)); } } }
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)); } } }
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)); } } } }
private void SelectFile(string directoryName, string fileName) { try { string[] extensions = options.Extension.Split(new char[] { ',', ';' }); foreach (var extension in extensions) { if (fileName.EndsWith(extension)) { if (options.IsSetOutput) { string outputBase = WildcardCharacterHelper.TranslateWildcardDirectoryPath(options.Output); DirectoryInfo outputBaseDir = new DirectoryInfo(outputBase); if (!outputBaseDir.Exists) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "No such directory -- {0}", outputBaseDir.FullName)); } string outputPath = GetDepthOutputDirectory(outputBase, directoryName); DirectoryInfo outputPathDir = new DirectoryInfo(outputPath); if (!outputPathDir.Exists) { outputPathDir.Create(); } if (options.IsSetMove) { File.Move(Path.Combine(directoryName, fileName), Path.Combine(outputPath, fileName)); OutputText(string.Format(CultureInfo.CurrentCulture, "Move file from -> {0}", Path.Combine(directoryName, fileName))); OutputText(string.Format(CultureInfo.CurrentCulture, " to -> {0}", Path.Combine(outputPath, fileName))); } else if (options.IsSetCopy) { File.Copy(Path.Combine(directoryName, fileName), Path.Combine(outputPath, fileName), true); OutputText(string.Format(CultureInfo.CurrentCulture, "Copy file from -> {0}", Path.Combine(directoryName, fileName))); OutputText(string.Format(CultureInfo.CurrentCulture, " to -> {0}", Path.Combine(outputPath, fileName))); } } } } } catch (NotSupportedException ex) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Operation exception -- {0}, {1}", Path.Combine(directoryName, fileName), ex.Message)); } catch (PathTooLongException ex) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Operation exception -- {0}, {1}", Path.Combine(directoryName, fileName), ex.Message)); } catch (FileNotFoundException ex) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Operation exception -- {0}, {1}", Path.Combine(directoryName, fileName), ex.Message)); } catch (UnauthorizedAccessException ex) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Operation exception -- {0}, {1}", Path.Combine(directoryName, fileName), ex.Message)); } catch (DirectoryNotFoundException ex) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Operation exception -- {0}, {1}", Path.Combine(directoryName, fileName), ex.Message)); } catch (SecurityException ex) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Operation exception -- {0}, {1}", Path.Combine(directoryName, fileName), ex.Message)); } catch (IOException ex) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Operation exception -- {0}, {1}", Path.Combine(directoryName, fileName), ex.Message)); } }