ID:2424819
 
(See the best response by Multiverse7.)
Code:


Problem description:
I am trying to get a list of files inside a directory is this possible?
Best response
This should do it:
proc/getallfiles(path, recursion = 1)
/*
Returns a list of all files (as file objects) in the directory path provided, as well as all files in any subdirectories, recursively!
The list returned is flat, so all items can be accessed with a simple loop.
This is designed to work with browse_rsc(), which doesn't currently support subdirectories in the browser cache.
*/


set background = 1
. = list()
for(var/f in flist(path))
if(copytext("[f]", -1) == "/")
if(recursion)
. += .("[path][f]")
else
. += file("[path][f]")


// Example that outputs all files in the game directory:
client/verb/printdir()
var/list/files = getallfiles(recursion = 0)
// Since recursion is set to 0, files in any subfolders are not included.
for(var/f in files)
world << "[f]"


Be careful where you use this. If recursion is enabled, this will continue without limits until every file is found! If you do something stupid, like run getallfiles("C:/"), it will run for a VERY, VERY, VERY long time! You have been warned.

If it makes you feel better, you can just set recursion = 0 in the definition.

Login to reply.