3.8.2 Managing Folder

Version Info

Minimum Origin Version Required: Origin 8 SR0

Get Project Tree

The following code shows how to access each folder in project explorer. Copy functions to Code Builder and compile, run ShowFolderTree in Command Window, will print out project tree.

void ShowFolderTree()
{
	Tree tr;
	
	Folder fldRoot = Project.RootFolder;
	TreeNode trRootFolder = tr.AddNode( fldRoot.GetName() );
	
	_get_project_tree(trRootFolder, fldRoot);
	out_tree(trRootFolder);
}

void _get_project_tree(TreeNode& tr, Folder& fldParent)
{	
	foreach(Folder fld in fldParent.Subfolders)
	{
		string strName = fld.GetName();
		strName.MakeValidCName();
		TreeNode trThisFolder = tr.AddNode( strName );
		trThisFolder.SetAttribute( STR_LABEL_ATTRIB, fld.GetName() );
		
		if( 0 != fld.Subfolders.Count() )
		{
			_get_project_tree(trThisFolder, fld);
		}			
	}
}

Create a Subfolder with the Specified Name

This example is used to create a subfolder under the active folder by specifying a name. If the specified subfolder exists already, delete it first, including its all subfolders and other items, and then create an empty subfolder under this active folder with this specified name.

void delete_and_create_subfolder(string strFolder)
{
	// trim white space
	strFolder.TrimRight();
	strFolder.TrimLeft();
	
	Folder fld = Project.ActiveFolder();  // get active folder in project explorer
	if(!fld.IsValid())
	{
		out_str("The active folder is not valid!");
		return;
	}
	
	// loop all subfolder
	foreach(Folder subfolder in fld.Subfolders)
	{
		if(0 == subfolder.GetName().Compare(strFolder))  // if the specified subfolder exists
		{
			fld.RemoveSubFolder(subfolder.GetName());  // delete this subfolder
			break;
		}	
	}
	
	fld.AddSubfolder(strFolder);  // add subfolder with the specified name
}