Exemplo n.º 1
0
        private static void PatternReplaceFunction(Dictionary <string, string> argsDict, string path)
        {
            var pattern = argsDict["-p"];
            var replace = argsDict["-r"];

            var files = IOAccess.GetFilesInPath(path);

            var matchedNames = new List <RenameInfo>();


            foreach (var file in files)
            {
                var fileInfo = new FileInfo(file);
                var matches  = Regex.Match(fileInfo.Name, pattern);

                var groups = matches.Groups.Cast <Group>().Where(g => g.Success).ToArray();

                if (matches.Success)
                {
                    var newName = Replacer.Replace(file, replace, groups);

                    matchedNames.Add(new RenameInfo()
                    {
                        OriginalName = fileInfo.Name, NewName = newName, FileFolderPath = fileInfo.DirectoryName
                    });
                }
            }
            ;

            if (argsDict.ContainsKey("-e"))
            {
                matchedNames.ForEach(nm =>
                {
                    var oldPath = Path.Combine(nm.FileFolderPath, nm.OriginalName);
                    var newPath = Path.Combine(nm.FileFolderPath, nm.NewName);
                    if (File.Exists(newPath))
                    {
                        Console.WriteLine($"renaming {nm.OriginalName} -> {nm.NewName} failed because a file with this name already exists!");
                    }
                    else
                    {
                        File.Move(oldPath, newPath);
                        Console.WriteLine($"renaming {nm.OriginalName} -> {nm.NewName}");
                    }
                });
            }
            else
            {
                matchedNames.ForEach(nm =>
                {
                    Console.WriteLine($"{nm.OriginalName} -> {nm.NewName}");
                });
            }
        }
Exemplo n.º 2
0
        private static void CheckPatternFunction(Dictionary <string, string> argsDict, string path)
        {
            var pattern = argsDict["-p"];
            var files   = IOAccess.GetFilesInPath(path);

            foreach (var file in files)
            {
                var fileInfo = new FileInfo(file);
                var matches  = Regex.Match(fileInfo.Name, pattern);

                var groups = matches.Groups.Cast <Group>().Where(g => g.Success).ToList();

                if (matches.Success)
                {
                    var groupString = string.Join(", ", groups);
                    Console.WriteLine($"{fileInfo.Name} -> Groups: {groupString}");
                }
            }
        }