Getting a list of folders
by Vern Jensen · in Torque Game Builder · 05/27/2008 (2:47 pm) · 6 replies
Is there any way to get a list of folders (directories) inside a given path?
I noticed an undocumented function called getDirectoryList, where you pass a path, and a depth. I tried this, but got only a blank line echoed to the console. (My folder structure has LevelScripts/global/paths/ and many subfolders inside paths whose names I want a list of.)
-Vern
I noticed an undocumented function called getDirectoryList, where you pass a path, and a depth. I tried this, but got only a blank line echoed to the console. (My folder structure has LevelScripts/global/paths/ and many subfolders inside paths whose names I want a list of.)
%list = getDirectoryList( "~/LevelScripts/global/paths/", 1 );
echo("Directory list: ");
echo(%list);-Vern
#2
Anyway, here is code that outputs a list of all folders in a given directory, going only one deep. (Turns out you need to pass 0 for this.)
Turns out getDirectoryList returns all the names separated by tabs, so the code above uses various string functions and the "\t" tab character to find each individual folder name in the string that's returned.
05/27/2008 (11:41 pm)
Sweet, that did the trick. I really never know when to use expandFilename() or not, since half the time it doesn't seem to be needed.Anyway, here is code that outputs a list of all folders in a given directory, going only one deep. (Turns out you need to pass 0 for this.)
%list = getDirectoryList( expandFilename("~/LevelScripts/"), 0 );
echo("Directory list: ");
%start = 0;
%index = strPos(%list, "\t", %start);
while ( %index != -1 )
{
%numChars = %index - %start;
%folderName = getSubStr(%list, %start, %numChars);
echo(%folderName);
%start = %index + 1; // Start of next folder name
%index = strPos(%list, "\t", %start);
}Turns out getDirectoryList returns all the names separated by tabs, so the code above uses various string functions and the "\t" tab character to find each individual folder name in the string that's returned.
#3
05/28/2008 (12:33 pm)
Hey Vern, this works but it leaves out the last folder in the directory. Just an FYI. :)
#4
05/28/2008 (1:38 pm)
Ahh, thanks for pointing that out! Here is the fixed version:%list = getDirectoryList( expandFilename("~/LevelScripts/"), 0 );
%start = 0;
%index = strPos(%list, "\t", %start);
while ( %index > %start )
{
%numChars = %index - %start;
%folderName = getSubStr(%list, %start, %numChars);
echo(%folderName);
%start = %index + 1; // Start of next folder name
%index = strPos(%list, "\t", %start);
if (%index == -1)
%index = strlen(%list);
}
#5
The function "getFieldCount" counts the number of tab seperated items in your string, and "getField" returns the ith field.
05/28/2008 (1:52 pm)
I think you can simplify things a bit there Vern.%list = getDirectoryList( expandFilename("~/LevelScripts/"), 0 );
for (%i = 0; %i < getFieldCount(%list); %i++)
{
%folderName = getField(%list, %i);
echo (%folderName);
}The function "getFieldCount" counts the number of tab seperated items in your string, and "getField" returns the ith field.
#6
05/28/2008 (2:12 pm)
Ooooh, far nicer. :-)
Torque Owner Ken Pajala
I think you need to call expandFilename() on any names that use the tilde.
%list = getDirectoryList( expandFilename("~/LevelScripts/global/paths/"), 1 ); echo("Directory list: "); echo(%list);That seems to work for me.
::ken::