コード例 #1
0
        public int Run(RunArgs ra)
        {
            Utils.ReadString("the path of the text file with the new file names: ", ref ra.State.FileNamesFilePath, true);
            Utils.ReadString("destination dir: ", ref ra.State.DestinationDir, true);
            Utils.ReadString("search pattern (*.*): ", ref ra.State.SearchPattern);
            Utils.ReadString("prefix: ", ref ra.State.Prefix);
            Utils.ReadString("name from file prefix: ", ref ra.State.NameFromFilePrefix);
            Utils.ReadInt("counter (0) : ", ref ra.State.NameCounter);
            Utils.ReadInt("counter step (1) : ", ref ra.State.NameCounterStep);
            Utils.ReadInt("number length with zero padding (6) : ", ref ra.State.PadZeroes);
            "sort options: no-0, asc name-1, desc name-2, randomize-3,  asc createdate-4, desc createdate-5. ".PrintLine();
            int sort = 0;

            Utils.ReadIntIn("sort first (no) : ", ref sort, new int[] { 0, 1, 2, 3, 4, 5 });
            ra.State.Sort = (SortType)sort;

            var useexisting = false;

            if (ra.State.Files != null && ra.State.Files.Length > 0)
            {
                string.Format(
                    "There are {0} files from a previous subprogram run. {1}The first of the old files: {2} ",
                    ra.State.Files.Length, Environment.NewLine, ra.State.Files[0]).PrintLine();
                useexisting = Utils.ReadWord("Use them? (y/*) ", "y");
            }

            if (!useexisting)
            {
                ra.State.Files = ra.RootDir.GetFiles(ra.State.SearchPattern, SearchOption.TopDirectoryOnly);
            }
            else if (Utils.ReadWord(string.Format("Change the root dir ({0}) ? (y/*)", ra.RootDir.FullName), "y"))
            {
                var newroot = string.Empty;
                Utils.ReadString("Enter new root dir: ", ref newroot, true);
                ra.ChangeRoot(newroot);
            }

            ra.State.SortFiles(ra.State.Sort);

            string.Format("There are {0} matches.", ra.State.Files.Length).PrintLine();
            ra.Trace = Utils.ReadWord("Trace first? (y/*): ", "y");

            var newNames = File.ReadAllLines(ra.State.FileNamesFilePath);

            if (newNames.Length < ra.State.Files.Length)
            {
                var msg = string.Format(
                    "The new names length is {0} and the files to be renamed are {1}. Abort? (y/*)",
                    newNames.Length,
                    ra.State.Files.Length);
                if (Utils.ReadWord(msg, "y"))
                {
                    return(-1);
                }
            }

            string.Format("Files - {0}, Names - {1}", newNames.Length, ra.State.Files.Length).PrintLine(ConsoleColor.Yellow);

            if (ra.Trace)
            {
                var tcounter = ra.State.NameCounter;
                var i        = 0;
                foreach (var f in ra.State.Files)
                {
                    if (f.FullName == ra.Me)
                    {
                        continue;
                    }
                    tcounter += ra.State.NameCounterStep;
                    var padded  = PadNumbers.PadZeroes(tcounter.ToString(), ra.State.PadZeroes);
                    var newname = string.Format("{0}{1}{2}{3}{4}", ra.State.Prefix, padded, ra.State.NameFromFilePrefix, newNames[i], f.Extension);
                    var newpath = Path.Combine(ra.State.DestinationDir, newname);
                    string.Format(f.Name + " --> " + newpath).PrintLine(ConsoleColor.Yellow);
                    i++;
                }
            }

            if (Utils.ReadWord("Rename all? (y/*): ", "y"))
            {
                var FI = new List <FileInfo>();
                var i  = 0;
                foreach (var f in ra.State.Files)
                {
                    if (f.FullName == ra.Me)
                    {
                        continue;
                    }
                    ra.State.IncrementCounter();
                    var padded  = PadNumbers.PadZeroes(ra.State.NameCounter.ToString(), ra.State.PadZeroes);
                    var newname = string.Format("{0}{1}{2}{3}{4}", ra.State.Prefix, padded, ra.State.NameFromFilePrefix, newNames[i], f.Extension);
                    var newpath = Path.Combine(ra.State.DestinationDir, newname);
                    File.Move(f.FullName, newpath);
                    FI.Add(new FileInfo(newpath));
                    i++;
                }
                ra.State.Files = FI.ToArray();
                $"Done - {ra.State.Files.Length} files renamed.".PrintLine();
            }
            else
            {
                "Aborting rename.".PrintLine();
            }

            return(0);
        }
コード例 #2
0
        public int Run(RunArgs ra)
        {
            bool   interactive = !ra.InArgs.ContainsKey("-ni");
            int    sort        = 4;
            string src         = null;

            ra.Trace             = false;
            ra.State.NameCounter = int.MinValue;

            if (interactive)
            {
                Utils.ReadString("Source dir (current): ", ref src);

                if (!string.IsNullOrEmpty(src))
                {
                    ra.ChangeRoot(src);
                }

                Utils.ReadString("Destination dir: ", ref ra.State.DestinationDir, true);
                Utils.ReadString("Search pattern (*.*): ", ref ra.State.SearchPattern);
                Utils.ReadString("Prefix: ", ref ra.State.Prefix);
                Utils.ReadInt("Counter start value (default is prefix only): ", ref ra.State.NameCounter);

                if (ra.State.NameCounter > int.MinValue)
                {
                    Utils.ReadInt("Counter step (1): ", ref ra.State.NameCounterStep);
                    Utils.ReadInt("Number length with zero padding (6): ", ref ra.State.PadZeroes);
                    "Sort options: no-0, asc name-1, desc name-2, randomize-3,  asc createdate-4, desc createdate-5. ".PrintLine();
                    Utils.ReadIntIn("Option (4): ", ref sort, new int[] { 0, 1, 2, 3, 4, 5 });
                }
            }
            else
            {
                ra.ChangeRoot(ra.InArgs.GetFirstValue("-src"));
                ra.State.DestinationDir = ra.InArgs.GetFirstValue("-dest");
                if (ra.InArgs.ContainsKey("-sp"))
                {
                    ra.State.SearchPattern = ra.InArgs.GetFirstValue("-sp");
                }
                if (ra.InArgs.ContainsKey("-prf"))
                {
                    ra.State.Prefix = ra.InArgs.GetFirstValue("-prf");
                }
                if (ra.InArgs.ContainsKey("-ic"))
                {
                    ra.State.NameCounter = int.Parse(ra.InArgs.GetFirstValue("-ic"));
                }
                if (ra.InArgs.ContainsKey("-step"))
                {
                    ra.State.NameCounterStep = int.Parse(ra.InArgs.GetFirstValue("-step"));
                }
                if (ra.InArgs.ContainsKey("-zpad"))
                {
                    ra.State.PadZeroes = int.Parse(ra.InArgs.GetFirstValue("-zpad"));
                }
                if (ra.InArgs.ContainsKey("-sort"))
                {
                    ra.State.PadZeroes = int.Parse(ra.InArgs.GetFirstValue("-sort"));
                }
            }

            ra.State.Sort  = (SortType)sort;
            ra.State.Files = ra.RootDir.GetFiles(ra.State.SearchPattern, SearchOption.TopDirectoryOnly);
            ra.State.SortFiles(ra.State.Sort);

            string.Format("There are {0} matches.", ra.State.Files.Length).PrintLine();

            if (interactive)
            {
                ra.Trace = Utils.ReadWord("Trace first? (y/*): ", "y");
            }

            string padded, newpath, newname;

            if (ra.Trace)
            {
                var tcounter = ra.State.NameCounter;
                foreach (var f in ra.State.Files)
                {
                    if (f.FullName == ra.Me)
                    {
                        continue;
                    }
                    if (ra.State.NameCounter > int.MinValue)
                    {
                        tcounter += ra.State.NameCounterStep;
                        padded    = PadNumbers.PadZeroes(tcounter.ToString(), ra.State.PadZeroes);
                        newname   = string.Format("{0}{1}{2}", ra.State.Prefix, padded, f.Extension);
                    }
                    else
                    {
                        newname = ra.State.Prefix + f.Name;
                    }

                    newpath = Path.Combine(ra.State.DestinationDir, newname);
                    string.Format(f.Name + " --> " + newpath).PrintLine(ConsoleColor.Yellow);
                }
            }

            if (!interactive || Utils.ReadWord("Rename all? (y/*): ", "y"))
            {
                var FI = new List <FileInfo>();
                foreach (var f in ra.State.Files)
                {
                    if (f.FullName == ra.Me)
                    {
                        continue;
                    }
                    if (ra.State.NameCounter > int.MinValue)
                    {
                        ra.State.IncrementCounter();
                        padded  = PadNumbers.PadZeroes(ra.State.NameCounter.ToString(), ra.State.PadZeroes);
                        newname = string.Format("{0}{1}{2}", ra.State.Prefix, padded, f.Extension);
                    }
                    else
                    {
                        newname = ra.State.Prefix + f.Name;
                    }

                    newpath = Path.Combine(ra.State.DestinationDir, newname);

                    File.Move(f.FullName, newpath);
                    FI.Add(new FileInfo(newpath));
                }
                ra.State.Files = FI.ToArray();
                $"Done - {ra.State.Files.Length} files renamed.".PrintLine();
            }
            else
            {
                "Aborting move.".PrintLine();
            }

            return(0);
        }