Stuck on MoveFileOrDirectory

Hi, this is what I’m trying to use to move a folder in C#:

As test I have 2 folders in /Assets:
A
b
No matter what I’ve tried, I get the rather non-explaining errors, like:
IOException: Failed to Move File / Directory from ‘/A’ to ‘/b/A’.

As far as I can tell, all of the following fail:

                      FileUtil.MoveFileOrDirectory("A", "b/A");
                      FileUtil.MoveFileOrDirectory("/A", "b/A");
                      FileUtil.MoveFileOrDirectory("A/", "b/A");
                      FileUtil.MoveFileOrDirectory("/A/", "b/A");

                      FileUtil.MoveFileOrDirectory("A", "/b/A");
                      FileUtil.MoveFileOrDirectory("/A", "/b/A");
                      FileUtil.MoveFileOrDirectory("A/", "/b/A");
                      FileUtil.MoveFileOrDirectory("/A/", "/b/A");

                      FileUtil.MoveFileOrDirectory("A", "/b/A/");
                      FileUtil.MoveFileOrDirectory("/A", "/b/A/");
                      FileUtil.MoveFileOrDirectory("A/", "/b/A/");
                      FileUtil.MoveFileOrDirectory("/A/", "/b/A/");

                      FileUtil.MoveFileOrDirectory("A", "b/");
                      FileUtil.MoveFileOrDirectory("/A", "b/");
                      FileUtil.MoveFileOrDirectory("A/", "b/");
                      FileUtil.MoveFileOrDirectory("/A/", "b/");

                      FileUtil.MoveFileOrDirectory("A", "/b");
                      FileUtil.MoveFileOrDirectory("/A", "/b");
                      FileUtil.MoveFileOrDirectory("A/", "/b");
                      FileUtil.MoveFileOrDirectory("/A/", "/b");

FileUtil.MoveFileOrDirectory(Application.dataPath + “/A”, Application.dataPath + “/b/A”);

FileUtil.MoveFileOrDirectory(Application.dataPath + “/A”, Application.dataPath + “/b”);

FileUtil.MoveFileOrDirectory(Application.dataPath + “/A”, Application.dataPath + “/b/”);

FileUtil.MoveFileOrDirectory(Application.dataPath + “/A/”, Application.dataPath + “/b”);

Any clues or actual working code (not like the Unity Docs(!)) would be much appreciated, thanks :slight_smile:

You have a couple lines of code in there that actually do work. Did you have them all in a line like this trying to execute after each other? If so, you wouldn’t have made it to the lines that do work. Alternatively, you may have not refreshed your AssetDatabase and not noticed it did work. (These are just guesses)

With folders named “A” and “b” in my root Assets folder, I was able to successfully use the following code to move folder A into b: (Note that I was unable to successfully use a relative path even though the documentation says it’s possible)

using UnityEngine;
using UnityEditor;

public static class MoveFolder
{
    [MenuItem("Example/Move A into b")]
    static void MoveAIntoB()
    {
        FileUtil.MoveFileOrDirectory(Application.dataPath + "/A", Application.dataPath + "/b/A");
        AssetDatabase.Refresh();
    }

    [MenuItem("Example/Move A out of b")]
    static void MoveAOutOfB()
    {
        FileUtil.MoveFileOrDirectory(Application.dataPath + "/b/A", Application.dataPath + "/A");
        AssetDatabase.Refresh();
    }
}

Hi @bellicapax thanks a lot for taking your time to try and solve this :slight_smile:

I’m ‘glad’ to see that you could not make a relative path work either, something’s wrong here.

I got the same errors as always, when running your code - I suspect you’re on a PC, I am testing on OSX right now, will try on PC later.

Am looking at filing a bug, any one else’s input would be appreciated.

Thanks.