Resources Folder Woes. How do I move stuff?

Good morning!

I’ve been toiling away with this for about 3 hours now and can’t seem to find a solution that works for me.

What I’m trying to do is search all of a project’s assets, pick out the audio clips, and then put them into the resources folder if they’re not already. Eventually I’ll just make a duplicate - but that’s for later.

My current approach:

static void ImportAudioClips(){
		string[] pathNames = AssetDatabase.GetAllAssetPaths();
		for(int i = 0; i < pathNames.Length;i++){
			string fileName = (Path.GetFileName(pathNames*));*
  •  	if(fileName.Contains("wav") || fileName.Contains("mp3")){*
  •  		fileName = fileName.Remove(fileName.Length - 4);*
  •  		aclips.Add(fileName); //This is a public list*
  •  	}*
  •  }*

This successfully sorts though the files and gets their names - but it doesn’t leave me with a way to reference OR move them.
So then I continue on and use Resources.Load() to load them all. Of course this only works if they’re in the resources folder. I’d love for them to be, but there doesn’t seem to be a way to reference the stinking resources folder without using some super convoluted techniques that are beyond me.l
Help is much appreciated!

You shouldn’t use System.IO to move files since it’s the same as moving them outside of Unity. You should use AssetDatabase.MoveAsset.

Also keep in mind that there is nothing like the resources folder since you have have multiple resources folders. They act like a single virtual resource library at runtime.

Like most AssetDatabase methods, MoveAsset also takes two asset paths:

All paths are relative to the project folder

So the paths you got back from GetAllAssetPaths are already relative to the project folder. Now you just need to setup your destination path. For example:

string destPath = "Assets/Resources/" + Path.GetFileName(srcPath));
AssetDatabase.MoveAsset(srcPath, destPath);

In addition you could check the source path if it contains “/Resources/” and skip the file if it’s already in a resources folder.

With the file paths you are left with after going through all the assets, you have all the data you need.
You can move the files just using the standard System.IO operations to move the file to a new location.

However I’m not sure how this will effect any existing links to those sounds that are already set up in your scenes/prefabs. I’d suggest also checking for and moving the meta files for those assets if they exist (not sure if this will be enough to maintain those links, but it might).