Ejemplo n.º 1
0
        /// <summary>
        /// Scrambles the names of the files in the directory
        /// </summary>
        /// <param name="progress">method(double) to call on progress updates</param>
        public void ScrambleNames(Action <double> progress = null)
        {
            try
            {
                Console.Log("FolderScrambler: Scrambling \"" + folderPath + "\"", Console.LogTypes.Info);

                int prog = 0;
                int max  = fileNames.Keys.Count;
                if (includeSubFolders)
                {
                    max += subFolders.Length;
                }
                foreach (string extension in fileNames.Keys)
                {
                    List <string> names = fileNames[extension];

                    List <int> randomNumbers = GetRandomNumbers(names.Count);

                    int randomNum;

                    for (int i = 0; i < names.Count && i < randomNumbers.Count; i++)
                    {
                        randomNum = randomNumbers[i];
                        //reverseFileNames.Add(names[randomNum], names[i]);
                        //Turns off the reverse scrambling ability
                        File.Move(CreateFullPath(names[i]), CreateFullPath(names[randomNum]) + tempExtension);
                    }
                    prog++;
                    //update progress
                    progress?.Invoke(prog / max);
                }

                CleanTempExtensions();

                if (includeSubFolders)
                {
                    foreach (DirectoryInfo directory in subFolders)
                    {
                        FolderScrambler sc = new FolderScrambler(directory.FullName, allExcept, extensions, includeSubFolders);
                        //scrambledSubFolders.AddLast(sc); For reversability
                        sc.ScrambleNames(null);
                        prog++;
                        progress?.Invoke(prog / max);
                    }
                }
                //ensure progress is complete
                progress?.Invoke(100);
                Console.Log("FolderScramlber: Completed scramble for \"" + folderPath + "\"", Console.LogTypes.Info);
            }
            catch (Exception e)
            {
                Console.LogException(e, "Check ScrambleNames()");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Runs the folderscrambler on a background thread
        /// </summary>
        /// <param name="inputRoot">Path to the whole corruption task to corupt</param>
        /// <param name="scrambleRoot">Path to just the rootfolder of the folder scramble task</param>
        /// <param name="destinationRoot">Root path where the corrupt job will be written</param>
        private void runFolderScramble(string inputRoot, string scrambleRoot, string destinationRoot)
        {
            try
            {
                int index = scrambleRoot.IndexOf(inputRoot);
                if (index < 0)
                {
                    MessageBox.Show("Folder Scramble directory is not a subfolder of the parent directory. Aborted.", "CrossCorrupt");
                    RunCorruptBtn.Text = "Run Corrupt";

                    Console.Log("UI: Unable to scramble folder, subdirectory " + scrambleRoot + " is not inside " + inputRoot, Console.LogTypes.Error);
                    return;
                }

                Console.Log("UI: Setting up folder scramble output directory ", Console.LogTypes.Info);

                //build the new destination folder by taking the common parts of the scrambleRoot (user picked) and the inputRoot
                var    split   = inputRoot.Split(Path.DirectorySeparatorChar);
                string newPath = destinationRoot + Path.DirectorySeparatorChar + (CustomNameText.Text.Length > 0 ? CustomNameText.Text + Path.DirectorySeparatorChar : split[split.Length - 1]) + Path.DirectorySeparatorChar + scrambleRoot.Substring(inputRoot.Length);
                //fix directoryseparatorchar duplicating characters
                newPath = newPath.Replace("//", "/");
                newPath = newPath.Replace("\\\\", "\\");
                Console.Log("UI: Folder scramble output directory = " + newPath, Console.LogTypes.Info);

                //build filetypes hashset
                HashSet <string> fileTypes = new HashSet <string>(FolderScrambleTypesTxt.Text.Replace(" ", "").Split(','));
                Console.Log("UI: FileTypes = " + FolderScrambleTypesTxt.Text + "; scrambling " + (!(bool)EnableSubfolderScramble.Checked ? "these types only" : "all except these types"), Console.LogTypes.Info);

                FolderScrambler fs = new FolderScrambler(newPath, (bool)FolderScrambleInvertChk.Checked, fileTypes, (bool)EnableSubfolderScramble.Checked);
                fs.ScrambleNames((double prog) =>
                {
                    //progress updates go here
                    //run UI things on the UI thread
                    Application.Instance.Invoke(() =>
                    {
                        MainProg.Value = (int)prog;
                        if (prog >= 100)
                        {
                            RunCorruptBtn.Text = "Run Corrupt";
                            running            = false;
                            Console.Log("UI: Folder Scramble completed!", Console.LogTypes.Info);
                            MessageBox.Show("FolderScramble complete!", "CrossCorrupt");
                        }
                    });
                });
            }catch (Exception e)
            {
                Console.LogException(e, "See RunFolderScramble");
            }
        }