Пример #1
0
        public static void RenameFilesInFolder(DirectoryInfo folder, RenameConfig config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            config.Validate();
            if (folder == null)
            {
                throw new ArgumentNullException("folder");
            }
            if (!folder.Exists)
            {
                throw new DirectoryNotFoundException($"Folder doesn't exist! ({folder.FullName})");
            }

            var filter = config.Filter;

            Common.Msg($"Renaming {filter} files in {folder.FullName}");
            Trace.Indent();

            if (string.IsNullOrWhiteSpace(filter))
            {
                filter = "*.*";
            }

            var files = Common.GetFiles(folder, filter);

            int fileCount = files.Count;
            int counter   = 1;

            Common.Progress(0);
            foreach (var file in files.OrderBy(f => f.CreationTimeUtc).ToList())
            {
                try
                {
                    RenameFile(file, counter, config, 100 * counter / fileCount);
                }
                catch (IOException)
                {
                    if (config.StopOnError)
                    {
                        throw;
                    }
                }
                counter++;
            }

            Trace.Unindent();
        }
Пример #2
0
        //public static void RenameFolder(string pattern, DirectoryInfo folder)
        //{
        //    var name = EvaluateFolderNamePattern(pattern, folder);
        //    folder.MoveTo(name);
        //}

        public static void RenameFile(FileInfo file, int counter, RenameConfig config, int progressPercent)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }
            if (!file.Exists)
            {
                throw new FileNotFoundException("File doesn't exist!", file.FullName);
            }

            var path = Path.GetDirectoryName(file.FullName);

            var origName = Path.GetFileNameWithoutExtension(file.Name);
            var newName  = EvaluateFileNamePattern(config.FilenamePattern, file, counter);

            if (config.FilenameCase == CharacterCasing.lower)
            {
                newName = newName.ToLower();
            }
            else if (config.FilenameCase == CharacterCasing.upper)
            {
                newName = newName.ToUpper();
            }

            var extension = Path.GetExtension(file.Name);

            if (config.ExtensionCase == CharacterCasing.lower)
            {
                extension = extension.ToLower();
            }
            else if (config.ExtensionCase == CharacterCasing.upper)
            {
                extension = extension.ToUpper();
            }

            Trace.WriteLine($"Renaming file {file.Name} to {newName + extension}");
            Common.Progress(progressPercent);
            file.MoveTo(Path.Combine(path, newName + extension));

            if (config.AdditionalFileTypes != null)
            {
                foreach (var type in config.AdditionalFileTypes)
                {
                    extension = type;
                    if (config.ExtensionCase == CharacterCasing.lower)
                    {
                        extension = extension.ToLower();
                    }
                    else if (config.ExtensionCase == CharacterCasing.upper)
                    {
                        extension = extension.ToUpper();
                    }

                    var origPath = Path.Combine(path, origName + type);
                    if (File.Exists(origPath))
                    {
                        var newPath = Path.Combine(path, newName + extension);
                        Trace.WriteLine($"Renaming file {origName + type} to {newName + extension}");
                        File.Move(origPath, newPath);
                    }
                }
            }
        }