public void Execute(IAppendFileNameWardenOptions options) { var files = _fs.DirectoryInfo .FromDirectoryName(options.Source) .EnumerateFiles("*", options.Search) .ToList(); var overwrittenFiles = new List <string>(); foreach (var file in files) { if (overwrittenFiles.Contains(file.FullName)) { continue; } var fileDirectory = file.DirectoryName; var fileNameWithoutExtension = _fs.Path.GetFileNameWithoutExtension(file.Name); var fileExtension = _fs.Path.GetExtension(file.Name); var fileNameWithSuffix = _appendFileNameStrategy.FormatFileName(fileNameWithoutExtension, fileExtension, options); var fileNameWithSuffixPath = _fs.Path.Combine(fileDirectory, fileNameWithSuffix); var fileNameWithSuffixInfo = _fs.FileInfo.FromFileName(fileNameWithSuffixPath); if (options.OverwriteExistingFiles && fileNameWithSuffixInfo.Exists) { overwrittenFiles.Add(fileNameWithSuffixPath); fileNameWithSuffixInfo.Delete(); } file.MoveTo(fileNameWithSuffixPath); } }
public string FormatFileName(string fileName, string extension, IAppendFileNameWardenOptions options) => $"{fileName}{options.Suffix}{extension}";