User Tools

Site Tools


List Relative Paths

Summary

Below is an example demonstrating how you can obtain a [recursive] list of relative file paths from a given relative base directory path.

API Areas of Interest

Example

List_Relative_Paths.dsa
// DAZ Studio version 4.9.0.38 filetype DAZ Script
 
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	/*********************************************************************/
	// Array<String> : A function for listing the files in a 
	function listRelativeContentFiles( sRelDirPath, aExtensions, nDirTypes, bRecursive )
	{
		// Get the content manager
		var oContentMgr = App.getContentMgr();
 
		// Let the user know we are busy
		setBusyCursor();
 
		// Get a list of URIs that meet our criteria - 4.9.0.38
		var aUris = oContentMgr.getDirectoryContentsUrisList( nDirTypes, [ sRelDirPath ], aExtensions );
 
		// Initialize a depth that does not filter
		var nSrcDepth = 0;
		// If we are not recursing
		if( !bRecursive ){
			// Get the depth of the source path
			nSrcDepth = sRelDirPath.split( "/" ).length;
		}
 
		// Declare working variables
		var oUri;
		var sFilePath;
		var aPathParts;
		var nResultDepth;
 
		// Get the number of URIs found
		var nUris = aUris.length;
 
		// Pre-size the list of results; avoid resizing
		var aResults = new Array( nUris );
 
		// Iterate over the list of URIs
		for( var i = 0; i < nUris; i += 1 ){
			// Get the 'current' URI
			oUri = aUris[ i ];
			// Get the relative file path from the URI
			sFilePath = oUri.filePath;
			// Split the path into its constituent parts
			aPathParts = sFilePath.split( "/" );
			// Get the depth of the path
			nResultDepth = aPathParts.length;
			// If we are not recursive and the file is deeper than the source
			if( nSrcDepth > 0 && nResultDepth > nSrcDepth ){
				//print( nSrcDepth, ":", nResultDepth, " - ", sFilePath );
				// Next!!
				continue;
			}
 
			// Capture the relative path
			aResults[i] = sFilePath;
		}
 
		// Remove 'empty' elements
		aResults = aResults.filter( Boolean );
 
		// Let the user know we are finished
		clearBusyCursor();
 
		// Return the results
		return aResults;
	};
 
	/*********************************************************************/
	// Define the relative path of the folder to get the contents of
	var sRelDirPath = "/People/Genesis 9/Characters/";
	// Define the extension(s) of the files we want to list
	var aExtensions = [ "duf" ];
 
	// Define the directory types we want to list
	var nDirTypes = DzContentMgr.CloudDB | DzContentMgr.NativeDirs;
 
	// Define whether or not we want a recursive list
	var bRecursive = false;
 
	// Get a list of relative paths
	var aRelPaths = listRelativeContentFiles( sRelDirPath, aExtensions, nDirTypes, bRecursive );
 
	// Provide feedback
	print( aRelPaths.join( "\n" ) );
 
// Finalize the function and invoke
})();