コード例 #1
0
        static public FileWalkerResult Walk(string path, ILogger logger)
        {
            FileWalkerResult result = new FileWalkerResult();

            ListFiles(path);

            return(result);

            void ListFiles(string currentPath)
            {
                foreach (var file in Directory.EnumerateFiles(currentPath))
                {
                    logger.WriteLine(file);
                    AddFile(file);
                }
                foreach (var dir in Directory.EnumerateDirectories(currentPath))
                {
                    logger.WriteLine(dir);
                    ListFiles(dir);
                }

                if (result.fileToPath.Count > FileWalkerConfig.MaxFileProcessed)
                {
                    throw new OperationCanceledException();
                }
            }

            void AddFile(string file)
            {
                string fileName   = Path.GetFileName(file);
                string folderName = Path.GetDirectoryName(file);

                logger.WriteLine(fileName);

                if (result.fileToPath.ContainsKey(fileName))
                {
                    result.fileToPath[fileName].Add(folderName);
                }
                else
                {
                    result.fileToPath[fileName] = new List <string>();
                    result.fileToPath[fileName].Add(folderName);
                }

                if (result.filesCountPerFolder.ContainsKey(folderName))
                {
                    result.filesCountPerFolder[folderName]++;
                }
                else
                {
                    result.filesCountPerFolder[folderName] = 1;
                }
            }
        }
コード例 #2
0
        public void DoWork(string path, ILogger logger)
        {
            FileWalkerResult res = null;

            try
            {
                res = FileWalker.Walk(path, logger);
            }
            catch (OperationCanceledException exc)
            {
                Console.WriteLine("Too much files");
            }
            FileWalkerAnalyzer.AnalyzeResult(res, logger);
        }
コード例 #3
0
        static public void AnalyzeResult(FileWalkerResult data, ILogger logger)
        {
            var duplicatesPerFolder = new Dictionary <string, int>();

            logger.WriteLine();
            logger.WriteLine("SUMMARY");
            logger.WriteLine();
            int dubbedKeys   = 0;
            int dubbedValues = 0;

            foreach (var item in data.fileToPath.Where(kv => kv.Value.Count > 2).Where(kv => !FileWalkerConfig.ignoredFileNames.Contains(kv.Key)))
            {
                dubbedKeys++;
                logger.WriteLine($"FILE: {item.Key} is in multiple folders:");
                foreach (var folder in item.Value)
                {
                    dubbedValues++;
                    logger.WriteLine(folder);
                    if (duplicatesPerFolder.ContainsKey(folder))
                    {
                        duplicatesPerFolder[folder]++;
                    }
                    else
                    {
                        duplicatesPerFolder[folder] = 1;
                    }
                }
            }
            logger.WriteLine();
            logger.WriteLine();
            logger.WriteLine($"You can remove {dubbedValues - dubbedKeys} files");


            logger.WriteLine($"Directories with duplicates ({duplicatesPerFolder.Keys.Count}):");
            foreach (var folder in duplicatesPerFolder.Keys.OrderBy(f => f))
            {
                float percentage = (float)duplicatesPerFolder[folder] / data.filesCountPerFolder[folder];
                logger.WriteLine($"{folder} is in {percentage.ToString("p")} duplicated");
            }
        }