Listing files on an NXT

(Sample code by Dave Smart taken from the DroidScript Google Group)

Here is an example of how to list all files on your Lego NXT brick:

//Called when application is started.
function OnStart()
{
	//Create our screen layout.
	CreateLayout();
 
	//Create NXT controller object.
	nxt = app.CreateNxt();   
}
 
//Create the screen layout.
function CreateLayout()
{	
	//Create the layout (with repeating pattern background).
	lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
	lay.SetBackground( "/Sys/Img/Tile.png", "repeat" );
 
	//Create 'Connect' button.
	btn = app.CreateButton( "Connect", 0.3,0.1, "Lego" );
	btn.SetMargins( 0,0,0,0.1 );
	btn.SetOnTouch( btn_OnTouch );  
	lay.AddChild( btn );
	     
	//Create 'List Files' button.
	btnCon = app.CreateButton( "List Files", 0.3, 0.1, "Lego" );    
	btnCon.SetOnTouch( btnCon_OnTouch );     
	lay.AddChild( btnCon ); 
 
	//Add layout to the app.
	app.AddLayout( lay );
}
 
//Called when user presses connect.
function btn_OnTouch()
{        
	//Show list of NXT devices.
	nxt.ShowDevices();
}
 
//Called when 'List Files' button is pressed.
function btnCon_OnTouch( e )
{                           
	alert( ListFiles( "*.*" ) );
}
 
//List files on the NXT using a search pattern.
//eg. "*.rso" finds all sound files.
function ListFiles( pattern )
{   
    app.ShowProgress("Working...");
    
    //Find first matching file on nxt.
    var list = "";
    var inf = nxt.FileFindFirst( pattern );
    if( inf!=null ) list = inf.GetName();    
    
    //Find remaining files.
    while( 1 ) {
	    inf = nxt.FileFindNext( inf.GetHandle() );
	    if( inf==null ) break;
	    list += "," + inf.GetName();	    
    }
    app.HideProgress();
    return list;
}