Hi, i am trying to name a list of prefab with the name of the folder which contains it.
So i start with this:
import System.IO;
var mainFolder = new DirectoryInfo("C:/path/path/");
var dirName = mainFolder.GetDirectories();
var posZ = -6.7;
var foldInstance : Transform;
function Start () {
for (directories in dirName){
Instantiate(foldInstance, new Vector3(-6.9,0.01,posZ), Quaternion.identity);
foldInstance.name = directories.ToString();
posZ++;
dirNum++;
}
}
This code look at the folders inside the specified path, instantiate as many objects as the folder it find and name it as the folders.
What i’d like to do is to erase the path from the name of the instantiated objects and leave only the name of the folder.
For example instead of “C:/path/path/Myfolder” should be "Myfolder.
import System.IO;
var mainFolder = new DirectoryInfo("C:/path/path/");
var dirName = mainFolder.GetDirectories();
var dirNameSolo : String = "The Path";
Path.GetFileName(dirNameSolo);
var posZ = -6.7;
var foldInstance : Transform;
function Start () {
for (directories in dirName){
Instantiate(foldInstance, new Vector3(-6.9,0.01,posZ), Quaternion.identity);
foldInstance.name = dirNameSolo;
posZ++;
dirNum++;
}
}
This return the path but not the name. It is the opposite I need
Then you can access the “filename” portion of those strings using the previously mentioned Path.GetFilename() method.
One the other hand, if you’re asking “How do I get a list of files in a given directory”, then you’ll want to look at Directory.GetFiles(). Like the Path class, the Directory class is also part of System.IO.
GetFiles() has a number of overloads, with the simplest being one that accepts a string representing a folder name and returns an array of strings representing the files in that folder.