- Home /
Stuck on MoveFileOrDirectory
Hi, this is what I'm trying to use to move a folder in C#:
https://docs.unity3d.com/ScriptReference/FileUtil.MoveFileOrDirectory.html
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 :)
Answer by bellicapax · Oct 10, 2016 at 03:45 PM
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();
}
}
Answer by Fritsl · Oct 10, 2016 at 03:58 PM
Hi @bellicapax thanks a lot for taking your time to try and solve this :)
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.
Hmm.. For the next in same situation, I made this work, have no time to investigate further:
FileUtil.CopyFileOrDirectory(sourceFolder, destinyFolder);
AssetDatabase.Refresh();
FileUtil.DeleteFileOrDirectory(sourceFolder);
AssetDatabase.Refresh();
sourceFolder / destinyFolder na$$anonymous$$g:
Assets/folder/folder/sourceFolder
Your answer
Follow this Question
Related Questions
missing files our project? 1 Answer
How to tell android to open a file 0 Answers
How Do I Write to a Created File? 1 Answer
Show files extensions in project window 2 Answers