Example #1
0
        public static void ArrangeImagePack(ConnectionViewModel cvm)
        {
            var alphabet = "abcdefghijklmnopqrstuvwxyz";

            var dir = string.Empty;

            using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
            {
                var result = dialog.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    dir = dialog.SelectedPath;
                }
                else
                {
                    return;
                }
            }

            var directories = CustomSearcher.GetDirectories(dir);

            if (!directories.Any())
            {
                MessageBox.Show($"No subdirectories are found in {dir}", "Problem", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }
            var ind_1st_char = 0;
            var ind_2nd_char = 0;
            var ren_dict     = new Dictionary <string, Dictionary <string, string> >();
            var global_cnt   = 0;

            foreach (var pack in directories)
            {
                var d     = new DirectoryInfo(pack);
                var files = d.GetFiles("*.jp*", SearchOption.TopDirectoryOnly).ToList();
                if (!files.Any())
                {
                    continue;
                }
                var char1 = alphabet[ind_1st_char];
                var char2 = alphabet[ind_2nd_char];
                if (ind_2nd_char == alphabet.Count() - 1)
                {
                    ind_1st_char++;
                    ind_2nd_char = 0;
                }
                else
                {
                    ind_2nd_char++;
                }

                var cnt = 0;
                ren_dict.Add(pack, new Dictionary <string, string>());
                foreach (var file in files.OrderBy(r => r.FullName))
                {
                    var new_fn_name = $"{file.DirectoryName}\\{char1}{char2}{cnt.ToString("D5")}{file.Extension}";
                    cnt++; global_cnt++;
                    ren_dict[pack].Add(file.FullName, new_fn_name);
                }
            }
            var res = MessageBox.Show($"You are about to rename {global_cnt} files in {ren_dict.Count} folders. Confirm?", "Confirmation", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);

            if (res != MessageBoxResult.Yes)
            {
                return;
            }

            var arranged_folder = $"{dir}\\Arranged";

            if (!Directory.Exists(arranged_folder))
            {
                Directory.CreateDirectory(arranged_folder);
            }
            var i   = 0;
            var tsk =
                Task.Factory.StartNew(() =>
            {
                try
                {
                    foreach (var pack in ren_dict)
                    {
                        foreach (var f in pack.Value)
                        {
                            File.Move(f.Key, f.Value);
                            File.Copy(f.Value, $"{arranged_folder}\\{Path.GetFileName(f.Value)}", true);
                            cvm.Status = $"Arranging file {++i}/{global_cnt}";
                        }
                    }
                }

                catch (Exception e)
                {
                    MessageBox.Show($"Problem renaming {e.Message}", "Problem", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }
                finally
                {
                    MessageBox.Show($"Done!", "Results", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                return;
            });

            while (!tsk.IsCompleted)
            {
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));
            }

            cvm.Status = "Ready.";
        }
Example #2
0
        public static void ReCreateImagePack(ConnectionViewModel cvm)
        {
            var dirArranged  = string.Empty;
            var dirImagePack = string.Empty;

            using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
            {
                dialog.Description = "Select arranged images folder";
                var result = dialog.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    dirArranged = dialog.SelectedPath;
                }
                else
                {
                    return;
                }
                dialog.Description = "Select image pack root folder";
                result             = dialog.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    dirImagePack = dialog.SelectedPath;
                }
                else
                {
                    return;
                }
            }
            var d             = new DirectoryInfo(dirArranged);
            var arrangedFiles = d.GetFiles("*.jp*", SearchOption.TopDirectoryOnly).ToList();

            if (!arrangedFiles.Any())
            {
                MessageBox.Show($"No images found in arranged folder {dirArranged}", "Problem", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }

            var directories = CustomSearcher.GetDirectories(dirImagePack);

            if (!directories.Any())
            {
                MessageBox.Show($"No subdirectories are found in {dirImagePack}", "Problem", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }
            var imagePackHashes = new Dictionary <string, Dictionary <string, List <string> > >();
            int i   = 0;
            var tsk =
                Task.Run(() =>
            {
                using (var md5 = MD5.Create())
                {
                    foreach (var pack in directories)
                    {
                        try
                        {
                            var dp    = new DirectoryInfo(pack);
                            var files = dp.GetFiles("*.jp*", SearchOption.TopDirectoryOnly).ToList();
                            if (!files.Any())
                            {
                                continue;
                            }
                            foreach (var file in files)
                            {
                                using (var stream = File.OpenRead(file.FullName))
                                {
                                    var hash = GetHash(md5, stream);
                                    if (!imagePackHashes.TryGetValue(hash, out Dictionary <string, List <string> > packFolders))
                                    {
                                        imagePackHashes.Add(hash, new Dictionary <string, List <string> >()
                                        {
                                            { dp.Name, new List <string>()
                                              {
                                                  file.FullName
                                              } }
                                        });
                                    }
                                    else
                                    {
                                        if (!packFolders.TryGetValue(dp.Name, out List <string> packFiles))
                                        {
                                            packFolders.Add(dp.Name, new List <string>()
                                            {
                                                file.FullName
                                            });
                                        }
                                        else
                                        {
                                            packFiles.Add(file.FullName);
                                        }
                                    }
                                }
                                cvm.Status = $"Getting MD5 checksum file {++i}";
                            }
                        }
                        catch (Exception e) { MessageBox.Show(e.Message); }
                    }

                    i = 0;
                    foreach (var file in arrangedFiles)
                    {
                        bool found = false;
                        Dictionary <string, List <string> > folderFile = null;
                        using (var stream = File.OpenRead(file.FullName))
                        {
                            var hash = GetHash(md5, stream);
                            if (imagePackHashes.TryGetValue(hash, out folderFile))
                            {
                                found = true;
                            }
                        }
                        if (found)
                        {
                            var fileMoved = false;
                            foreach (var pathName in folderFile.Keys)
                            {
                                var newPath = Path.Combine(dirArranged, pathName);
                                if (!Directory.Exists(newPath))
                                {
                                    Directory.CreateDirectory(newPath);
                                }
                                if (!fileMoved)
                                {
                                    File.Move(file.FullName, Path.Combine(newPath, Path.GetFileName(file.FullName)));
                                    fileMoved = true;
                                }
                            }
                        }
                        cvm.Status = $"Processing MD5 checksum of arranged file {++i}/{arrangedFiles.Count}";
                    }
                }
            });

            while (!tsk.IsCompleted)
            {
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));
            }

            cvm.Status = "Ready.";
        }