Remove part of a string from the name of an instantiated object

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.

Can you help?

Assuming you have a string variable containing something like:

string myPath = @"c:\some\test\folder";

then…

Path.GetFileName(myPath);

will return the last element of the passed path. In the above case, that would be:

folder

The Path class is part of System.IO;

Jeff

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 :slight_smile:

So, in my previous example, if you want the “c:\some\test” portion, you want:

Path.GetDirectoryName(myPath);

Jeff

Thank you Jeff, now i understand. I was printing the wrong variable in debug console. I have this code:

#pragma strict

import System.IO;

var mainFolder = new DirectoryInfo("D:/Test/Unity/here/thisone/lastone");


private var myReturn : String = "empty";

function Start () {

myReturn = Path.GetFileName(mainFolder.ToString());

print (myReturn);

}

function Update () {

}

The console returns “lastone” (without quotes), which is correct.

A related question: if the last folder of the path contains files, is there a way to have the names of these files without the path?

Thank you again

Most certainly, the answer is Yes, though I’m not sure I understand what you’re asking.

If you already have a collection of strings that represent paths and filenames, such as:

c:\some\folder\file1.txt
c:\some\folder\file2.txt
c:\some\folder\file3.txt

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.

You can check out the various overloads here:

http://msdn.microsoft.com/en-us/library/System.IO.Directory.GetFiles%28v=vs.100%29.aspx

Jeff

Ok, i got it! Thank you very much!