コード例 #1
0
        private void AddRule()
        {
            var rule = new RenameRule
            {
                Enabled  = true,
                Contains = "<Contains>",
                Format   = "<Format>",
                Pattern  = "<Pattern>",
            };

            renameRuleBindingSource.Add(rule);

            var row = GetRow(rule);

            if (row != null)
            {
                gridRules.ClearSelection();
                gridRules.FirstDisplayedScrollingRowIndex = row.Index;
                row.Selected = true;
            }
        }
コード例 #2
0
ファイル: Renamer.cs プロジェクト: allenm84/ma-renameFiles
        private static IEnumerable <RenameResult> Run(DirectoryInfo dir, RenameRule rule)
        {
            if (!dir.Exists)
            {
                yield break;
            }

            var files = dir
                        .EnumerateFiles()
                        .Where(f => Regex.IsMatch(f.Name, rule.Contains, RegexOptions.IgnoreCase))
                        .ToArray();

            var format = rule.Format;

            foreach (var file in files)
            {
                var regex = new Regex(rule.Pattern, RegexOptions.IgnoreCase);
                var m     = regex.Match(file.Name);
                if (m.Success)
                {
                    var groups = m.Groups.OfType <Group>().ToList();
                    var values = new Dictionary <int, object>();

                    var names = regex.GetGroupNames();
                    foreach (var name in names)
                    {
                        var group      = m.Groups[name];
                        var index      = groups.IndexOf(group);
                        var token      = $"${name}";
                        var tokenIndex = format.IndexOf(token);
                        if (tokenIndex >= 0)
                        {
                            var formatIndex = $"{{{index}}}";
                            var afterToken  = tokenIndex + token.Length;
                            if (afterToken < format.Length && format[afterToken] == '#')
                            {
                                // this is a number!
                                values[index] = $"{int.Parse(group.Value):000}";
                                token         = $"{token}#";
                            }
                            format = format.Replace(token, formatIndex);
                        }
                    }

                    var args = groups
                               .Select((g, i) => values.TryGetValue(i, out object value) ? value : g.Value)
                               .ToArray();

                    var filename = string.Format(format, args);
                    var filepath = Path.Combine(file.Directory.FullName, filename);
                    var result   = new RenameResult(file.Name, filename);
                    try
                    {
                        file.MoveTo(filepath);
                    }
                    catch
                    {
                        continue;
                    }
                    yield return(result);
                }
            }
        }
コード例 #3
0
 private DataGridViewRow GetRow(RenameRule rule)
 {
     return(gridRules.Rows
            .OfType <DataGridViewRow>()
            .FirstOrDefault(r => ReferenceEquals(r.DataBoundItem, rule)));
 }