Пример #1
0
        /// <summary>
        /// From a given name, increment it
        /// myFileAsset    ->  myFileAsset 1
        /// myFileAsset 58    ->  myFileAsset 59
        /// </summary>
        /// <param name="nameToIncrement">name to increment</param>
        /// <param name="index">new index incremented</param>
        /// <returns>new incremented name</returns>
        public static string IncrementName(string nameToIncrement, out int index)
        {
            index = ExtString.ExtractIntFromEndOfString(nameToIncrement);

            string nameWithoutNumber = nameToIncrement.Replace(index.ToString(), "");

            nameWithoutNumber = nameWithoutNumber.TrimEnd(' ');

            index += 1;

            return(nameWithoutNumber + " " + index);
        }
Пример #2
0
        /// <summary>
        /// from a given string, like: RaceTrack 102
        /// extract the number 102 (int)
        /// </summary>
        /// <param name="input"></param>
        /// <returns>number in the end of string</returns>
        public static int ExtractIntFromEndOfString(string input)
        {
            var stack = new Stack <char>();

            for (var i = input.Length - 1; i >= 0; i--)
            {
                if (!char.IsNumber(input[i]))
                {
                    break;
                }

                stack.Push(input[i]);
            }

            string result = new string(stack.ToArray());

            return(ExtString.ToInt(result));
        }
Пример #3
0
        /// <summary>
        /// take a path of a file, increment his number in his name, test if it exist, and return the new path
        ///
        /// use: string newFilePath = ExtString.RenameIncrementalFile(AssetDatabase.GetAssetPath(assetObject));
        /// if you want to have only the name after, do:
        /// string only name = Path.GetFileName(newFilePath);
        ///
        /// return the renamed file asset like:
        /// Asset/Folder/myFileAsset.asset      ->  Asset/Folder/myFileAsset 1.asset
        /// Asset/Folder/myFileAsset 0.asset    ->  Asset/Folder/myFileAsset 1.asset
        /// Asset/Folder/myFileAsset 58.asset    ->  Asset/Folder/myFileAsset 59.asset
        /// Asset/Folder/myFileAsset58.asset    ->  Asset/Folder/myFileAsset 59.asset
        /// asset 24.asset
        /// </summary>
        /// <param name="pathFileToRename">path of the file</param>
        /// <param name="index">new index calculated</param>
        /// <returns></returns>
        public static string RenameIncrementalFile(string pathFileToRename, out int index, bool setZeroIfNothingFound)
        {
            string nameAsset      = Path.GetFileNameWithoutExtension(pathFileToRename);
            int    numberPrevious = ExtString.ExtractIntFromEndOfString(nameAsset);

            string nameWithoutNumber = nameAsset.Replace(numberPrevious.ToString(), "");

            nameWithoutNumber = nameWithoutNumber.TrimEnd(' ');

            string dirName = Path.GetDirectoryName(pathFileToRename).Replace('\\', '/') + '/';

            string newName          = "";
            string finalRenamedPath = "";

            index = numberPrevious;

            do
            {
                if (index == 0)
                {
                    if (setZeroIfNothingFound)
                    {
                        newName = nameWithoutNumber + " " + (index);
                    }
                    else
                    {
                        newName = nameWithoutNumber;
                    }
                }
                else
                {
                    newName = nameWithoutNumber + " " + (index);
                }
                finalRenamedPath = dirName + newName + Path.GetExtension(pathFileToRename);
                index++;
            }while (File.Exists(finalRenamedPath) || Directory.Exists(finalRenamedPath));
            return(finalRenamedPath);
        }