Skip to content

Ability to duplicate folder but with new guid #8

Open
@shaharbar2

Description

@shaharbar2

Created code for that and extending from outside the code (static)
Wanted to contribute and open PR but I see there ois no code in the repo, attach it here
@jeffjadulco

public class DuplicateAndRegenerateGUIDFolder
    {
        [MenuItem("Assets/Regenerate GUID/ Duplicate and Regenerate GUID for Folder")]
        public static void CopyRegenerateAndPasteFolder()
        {
            var selectedGUIDs = Selection.assetGUIDs;

            if (selectedGUIDs.Length == 0 || !AssetGUIDRegeneratorMenu.RegenerateGUID_Validation())
            {
                Debug.LogWarning("No valid folder selected.");
                return;
            }

            var selectedPath = AssetDatabase.GUIDToAssetPath(selectedGUIDs[0]);
            if (!File.GetAttributes(selectedPath).HasFlag(FileAttributes.Directory))
            {
                Debug.LogWarning("Selected asset is not a folder.");
                return;
            }

            var folderTempPath = Path.Combine(Application.temporaryCachePath, Path.GetFileName(selectedPath));

            var newFolderName = Path.GetFileName(folderTempPath) + "_Copy";

            if (Directory.Exists(newFolderName) || Directory.Exists(folderTempPath))
            {
                Debug.LogWarning($"A folder with name {newFolderName} already exists in the destination.");
                DeleteTempFolder(folderTempPath);
                return;
            }

            CopyDirectory(selectedPath, folderTempPath);

            AssetGUIDRegeneratorMenu.RegenerateGUIDWithFolders_Implementation();

            var parentDirectory = Directory.GetParent(selectedPath).FullName;
            PasteFolder(folderTempPath, parentDirectory);

            DeleteTempFolder(folderTempPath);
        }

        private static void DeleteTempFolder(string folderCopyPath)
        {
            if (Directory.Exists(folderCopyPath))
            {
                Directory.Delete(folderCopyPath, true);
            }
        }

        private static void CopyDirectory(string sourceDir, string destinationDir)
        {
            Directory.CreateDirectory(destinationDir);
            foreach (var file in Directory.GetFiles(sourceDir))
            {
                var dest = Path.Combine(destinationDir, Path.GetFileName(file));
                File.Copy(file, dest);
            }

            foreach (var dir in Directory.GetDirectories(sourceDir))
            {
                var dest = Path.Combine(destinationDir, Path.GetFileName(dir));
                CopyDirectory(dir, dest);
            }
        }

        private static void PasteFolder(string sourceDir, string destinationParent)
        {
            var newFolderName = Path.GetFileName(sourceDir) + "_Copy";
            var newFolderPath = Path.Combine(destinationParent, newFolderName);

            if (Directory.Exists(newFolderPath))
            {
                Debug.LogWarning($"A folder with name {newFolderName} already exists in the destination.");
                return;
            }

            CopyDirectory(sourceDir, newFolderPath);
            AssetDatabase.Refresh();
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions