Exemplo n.º 1
0
        /// <summary>
        /// Populates the <c>_ComicsNewPaths</c> dictionary with all the comics that need to be moved.
        /// It does so in a way that all comics that need to be moved appear just once in the entire dictionary,
        /// this way we can move all comics asynchronously.
        /// </summary>
        void PopulateDictionary()
        {
            ConsoleUtilities.InfoMessage("Scanning directories...");
            try
            {
                Environment.CurrentDirectory = Path.GetPathRoot(Environment.SystemDirectory);
                if (_DoGroups)
                {
                    GetPreviousToDictionary();
                }

                foreach (var subDirectoryPath in Directory.EnumerateDirectories(_MainPath))
                {
                    string subDirectoryName = Path.GetFileName(subDirectoryPath);

                    //If it isn't a directory with files already organized, organize it
                    if (_AlreadyOrganizedRegex.IsMatch(subDirectoryName))
                    {
                        continue;
                    }

                    for (int i = 0; i < _Regices.Count(); i++)
                    {
                        Regex regex = _Regices[i];
                        if (!regex.IsMatch(subDirectoryName))
                        {
                            continue;
                        }

                        int[] idsGroup = regex.GetGroupNumbers();
                        (string groupName, string artistName, string comicName) =
                            GetComicInfo(idsGroup, regex.Match(subDirectoryName).Groups);

                        (string groupPath, string artistPath) = CreatePaths(groupName, artistName);

                        string artistFinalPath = Path.Combine(artistPath, comicName);
                        string groupFinalPath  = Path.Combine(groupPath, comicName);

                        InitializeKeyIfNotExists(artistPath);
                        _ComicsNewPaths[artistPath].Add(new ComicInfo(subDirectoryPath, artistFinalPath, artistName));

                        if (!_DoGroups || Directory.Exists(artistPath))
                        {
                            break;
                        }


                        InitializeKeyIfNotExists(groupPath);
                        _ComicsNewPaths[groupPath].Add(new ComicInfo(subDirectoryPath, groupFinalPath, artistName));

                        if (_ComicsNewPaths[artistPath].Count < _MinNumberOfComics)
                        {
                            break;
                        }

                        //Removes all references to previous comics that belonged to the same artist and group,
                        //which makes it safe to move all comics asynchonously
                        if (_ComicsNewPaths.TryGetValue(groupPath, out List <ComicInfo> listOfPaths))
                        {
                            listOfPaths.RemoveAll(info => info.ArtistName.Equals(artistName));
                        }

                        break;
                    }
                }
                ConsoleUtilities.InfoMessage("Finished!");
            }
            catch (Exception e)
            {
                ConsoleUtilities.ErrorMessage("An error ocurred...");
                Console.Error.WriteLine(e.Message);
            }
        }