Skip to content

Ability to duplicate folder but with new guid #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
shaharbar2 opened this issue Dec 12, 2023 · 0 comments
Open

Ability to duplicate folder but with new guid #8

shaharbar2 opened this issue Dec 12, 2023 · 0 comments

Comments

@shaharbar2
Copy link

shaharbar2 commented Dec 12, 2023

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();
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant