示例#1
0
        public List <MediaFileInfo> RetrieveMediaFiles(ClassifierArgs args)
        {
            IEnumerable <string> files;

            if (args.RecursiveMode == RecursiveMode.RootFolder)
            {
                MoveFilesToRoot(args);
                files = Directory.GetFiles(args.DirectoryPath, searchPattern: "*.*", searchOption: SearchOption.AllDirectories);
            }
            else
            {
                files = Directory.EnumerateFiles(args.DirectoryPath);
            }
            var images = files.Select(f =>
            {
                var fileInfo = new FileInfo(f);
                return(new MediaFileInfo
                {
                    CurrentPath = fileInfo.FullName,
                    FileName = fileInfo.Name,
                    ModifiedDate = fileInfo.LastWriteTime,
                });
            }).ToList();

            return(images);
        }
示例#2
0
        public void OrganizeDirectory(ClassifierArgs args)
        {
            var files = _storageUtilities.RetrieveMediaFiles(args);

            _classifier.Classify(files, args);
            _storageUtilities.MoveFiles(files);
        }
示例#3
0
        public void MoveFilesToRoot(ClassifierArgs args)
        {
            var files = Directory.GetFiles(args.DirectoryPath, searchPattern: "*.*", searchOption: SearchOption.AllDirectories);

            foreach (var file in files)
            {
                try
                {
                    var newPath = Path.Combine(args.DirectoryPath, new FileInfo(file).Name);
                    File.Move(file, newPath);
                }
                catch (IOException)
                {
                    HandleNameCollision(args, file);
                }
            }

            Console.WriteLine($"Moved {files.Length} to root directory");
        }
示例#4
0
        private void HandleNameCollision(ClassifierArgs args, string file)
        {
            var fileInfo = new FileInfo(file);
            var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileInfo.FullName);
            var suffix = 1;

            while (true)
            {
                var newPath = $"{fileNameWithoutExtension}-{suffix}{fileInfo.Extension}";
                Console.WriteLine($"Name conflict for {fileInfo.Name}");
                if (File.Exists(Path.Combine(args.DirectoryPath,
                                             newPath)))
                {
                    suffix++;
                }
                else
                {
                    File.Move(file, newPath);
                    break;
                }
            }
        }