Possible to move directory without reimporting assets?

When I drag a sub directory from a directory to another in the Project view, the directory gets moved instantly-ish. When I move the same sub directory using an editor script, Unity reimports all of the assets in the moved directory.

I have tried two different ways of moving files in my script:

FileUtil.MoveFileOrDirectory("Assets/Resources/DirName", "Assets/Audio/DirName");

And

try {
    Directory.Move("Assets/Resources/DirName", "Assets/Audio/DirName");
}
catch (Exception e) {
    Debug.Log("Move failed: "+e.Message);
}

Is there any way of moving a folder using a script, without triggering a reimport?

The solution was to move the files using AssetDatabase.MoveAsset(). That will make sure to keep the meta files intact and it doesn’t trigger a reimport.

Here’s an example:

DirectoryInfo dir = new DirectoryInfo("Assets/Dir");
FileInfo[] files = dir.GetFiles("*.wav");

foreach (FileInfo file in files) {
    string status = AssetDatabase.MoveAsset(
                        "Assets/Dir/" + file.Name,
                        "Assets/NewDir/" + file.Name);
    
    // Everything is OK if status is empty
    if (status != "") {
        Debug.Log(status);
    }
}

Remember to include

using System.IO;

at the top of your class.

I was wondering how to move folders with AssetDatabase.MoveAsset() for a long time.

Finally, I have an answer. To do this, you need to create a destination folder hierarchy. For example:

This code will only work if a folder Assets/WebGLTemplates/Resources exists.

AssetDatabase.MoveAsset("Assets/Resources/sounds", "Assets/WebGLTemplates/Resources/sounds");

The only relevant documentation that I’m aware of says to only move folders from within the editor.

"Never move any assets or organize this folder from the Explorer (Windows) or Finder (OS X). Always use the Project View!

There is a lot of meta data stored about relationships between asset files within Unity. This data is all dependent on where Unity expects to find these assets. If you move an asset from within the Project View, these relationships are maintained. If you move them outside of Unity, these relationships are broken. You’ll then have to manually re-link lots of dependencies, which is something you probably don’t want to do."

Calling functions to do the moving is most likely the equivalent of using Explorer or Finder.