Ejemplo n.º 1
0
        protected static IEnumerable <IStorageItem> FetchItems(IFolder rootFolder, RenameFlags renameFlags)
        {
            var items = new List <IStorageItem>();

            if (renameFlags.HasFlag(RenameFlags.Files))
            {
                items.AddRange(rootFolder.GetFiles());
            }

            IEnumerable <IFolder> folders = null;

            if (renameFlags.HasFlag(RenameFlags.Folder))
            {
                items.AddRange(folders ?? (folders = rootFolder.GetFolders()));
            }
            if (renameFlags.HasFlag(RenameFlags.SubFolders))
            {
                foreach (var folder in folders ?? rootFolder.GetFolders())
                {
                    items.AddRange(FetchItems(folder, renameFlags));
                }
            }
            return(items);
        }
Ejemplo n.º 2
0
        protected IEnumerable <MoveOrder> ProcessReplace
            (IEnumerable <IStorageItem> items, IEnumerable <ReplacePattern> patterns,
            RenameFlags renameFlags, string rootFolder = null)
        {
            items = items.GroupBy(i => i.Parent.Path).NaturalOrderByDescending(g => g.Key).
                    SelectMany(g => g.NaturalOrderBy(i => i.Path));

            var orderList = new List <MoveOrder>();

            foreach (var storageItem in items)
            {
                var order = new MoveOrder {
                    Path = storageItem.Path
                };
                try
                {
                    if (!storageItem.Exist)
                    {
                        throw new Exception($"'{storageItem.Path}' not exist");
                    }

                    var usePurename = (storageItem is IFile fileItem) && !renameFlags.HasFlag(RenameFlags.Extension);
                    var name        = usePurename ? (storageItem as IFile).PureName : storageItem.Name;
                    var destName    = name.Replaces(patterns) + (usePurename ? (storageItem as IFile).Extension : "");
                    var destPath    = Path.Combine(storageItem.Parent.Path, destName);

                    order.DestPath = destPath;

                    if (StorageHelper.Exist(destPath))
                    {
                        throw new Exception($"A file with same path '{destPath}' already exist");
                    }
                    if (orderList.Any(o => string.Equals(o.DestPath, destPath, StringComparison.OrdinalIgnoreCase)))
                    {
                        throw new Exception($"Some other file will rename to {destPath}");
                    }
                }
                catch (Exception err)
                {
                    order.Message = $"{err.GetType().FullName}: {err.Message}";
                }
                if (order.Path != order.DestPath)
                {
                    orderList.Add(order);
                }
            }

            using (var db = new RenameDbContext())
            {
                db.Processes.Insert(new RenameProcess()
                {
                    DateTime    = DateTime.Now,
                    RootFolder  = rootFolder,
                    Items       = items.Select(i => i.Path),
                    Patterns    = patterns,
                    RenameFlags = renameFlags
                });
            }

            return(orderList);
        }