string[] FilterFiles(string[] files) { return(files.Where(p => { var extension = GetFullFileExtension(Path.GetFileName(p)); return IncludeExtensions.Contains(extension) && !ExcludeExtensions.Contains(extension); }).ToArray()); }
public bool StartRenaming(string sourcePath, string destinationPath) { if (string.IsNullOrEmpty(sourcePath)) { throw new ArgumentException("sourcePath cannot be null"); } if (string.IsNullOrEmpty(destinationPath)) { throw new ArgumentException("destinationPath cannot be null"); } if (!Directory.Exists(sourcePath)) { throw new DirectoryNotFoundException(string.Format("sourcePath '{0}' could not be found", sourcePath)); } if (!Directory.Exists(destinationPath)) { throw new DirectoryNotFoundException(string.Format("destinationPath '{0}' could not be found", destinationPath)); } var startFileNames = Directory.GetFiles(sourcePath); foreach (string fileName in startFileNames) { var currentFile = new FileInfo(fileName); var currentFileExtension = currentFile.Extension.ToLower(); // Set new filepath and apply filename rename var destinationFileName = currentFile.Name; foreach (var renameWord in RenameWords) { destinationFileName = StringHelpers.ReplaceEx(destinationFileName, renameWord.Key, renameWord.Value); } var destinationFilePath = Path.Combine(destinationPath, destinationFileName); if (ExcludeExtensions.Contains(currentFileExtension)) { // Just copy the file to the destination, and rename the filename File.Copy(currentFile.FullName, destinationFilePath); } else if (RemoveExtensions.Contains(currentFileExtension)) { continue; // Skip this } else if (RenameExtensions.Contains(currentFileExtension)) { // Rename inside the file, and the filename string fileContent = ""; if (UtfExtensions.Contains(currentFileExtension)) { fileContent = File.ReadAllText(currentFile.FullName, System.Text.Encoding.UTF8); } else { fileContent = File.ReadAllText(currentFile.FullName); } foreach (var renameWord in RenameWords) { fileContent = StringHelpers.ReplaceEx(fileContent, renameWord.Key, renameWord.Value); } if (UtfExtensions.Contains(currentFileExtension)) { File.WriteAllText(destinationFilePath, fileContent, System.Text.Encoding.UTF8); } else { File.WriteAllText(destinationFilePath, fileContent); } } } var startDirectories = Directory.GetDirectories(sourcePath); foreach (var directoryName in startDirectories) { var currentDirectory = new DirectoryInfo(directoryName); // Directory needs to be renamed var destinationDirectoryName = currentDirectory.Name; foreach (var renameWord in RenameWords) { destinationDirectoryName = StringHelpers.ReplaceEx(destinationDirectoryName, renameWord.Key, renameWord.Value); } string destinationDirectoryPath = Path.Combine(destinationPath, destinationDirectoryName); Directory.CreateDirectory(destinationDirectoryPath); StartRenaming(currentDirectory.FullName, destinationDirectoryPath); } return(true); }