File Browser

I’m am working on implementing a file browser into my radio so people can listen to music on their HDD also but I’m having a little trouble figuring out how to do so in a simple way. Ive got a ghetto script thats partially working but its annoying me and i figure their has to be a easier way to browse around than somthing along the lines of which is what ive been working on now, so anyone got a simpler way to do this?

no ideas… :frowning:

Don’t use a DListView. Use a DTree.

For an example, use this:
Look to the File Browsing section in the gadmenu.lua. It supports music, text, models, materials, and maps.

Or look at the Expression 2 file browser. I used that to create the one above.

I dont really want to use DTree as that would mean i would have to list the entire hdd which I believe would be slow with lua :stuck_out_tongue:

DList in format of:
… (dir back button)

would be the fastest imo unless its possible to just enumerate the folders starting from C: and move foward from there and not list in somthing that hasnt been clicked, thanks tho ill look into it.

Nonono, DTree is much more intuitive and looks better (it’s meant to be a file browser). You don’t have to load every last file in your gmod folder; otherwise the File Browsing menu in the GAD would take like 5 minutes. How you do it is you load the folders, and then when you select a folder it loads the contents of the folder. It doesn’t take twenty minutes to load a couple folders.

edit This is the core of the tree, taken from the E2 code (<3 syranide):

[lua]local function setTree(dir, parent)
if(dir==nil) then return end
parent.ChildNodes = nil
local pFolders = file.FindDir(dir … “/*”)
for k, v in ipairs( pFolders ) do
local pFolder = parent:AddNode( v )
pFolder.FileDir = dir … “/” … v
pFolder.IsFile = false
pFolder.Name = v
pFolder.DoClick = function() setTree( pFolder.FileDir, pFolder ) end
for _,v in ipairs(filetypes) do DetectFile(v[1],dir,parent,v[2]) end

setTree( “…”,tree)[/lua]

And as you can see, I made an easy to use “DetectFile” function, so you can do shit with different types of files.

[lua]local function DetectFile(type,dir,parent,icon)
if type == “.txt” and !string.match(dir, “/data”) then return end
local pFiles = file.Find( dir … “/*” … type)
for _,v in ipairs(pFiles) do
local filenode = parent:AddNode( GetName(v,type) )
filenode.FileDir = dir … “/” … v
if type == “” then filenode.IsFile = false
filenode.IsFile = true
filenode.Icon:SetImage( icon )
filenode.Name = GetName(v,type)
filenode.DoClick = function()
if type == “.mp3” or type == “.wav” then
if table.Count(sounds) > 0 then sounds[1]:Stop() end
sounds = {}
sound = CreateSound(LocalPlayer(),filenode.FileDir)

local filetypes = {
{ “.wav”, “gui/silkicons/sound”,
[[All the sound files with .wav extensions are supported!
To play sound, click on the icon in the menu.
You can stop and start sounds with the corresponding buttons.

{ “.mp3”, “gui/silkicons/sound”,
[[All the sound files with .mp3 extensions are supported!
To play sound, click on the icon in the menu.
You can stop and start sounds with the corresponding buttons.


I cut out everything but the mp3/wav files. So basically, it finds al l the sound files in a folder, creates a subnode with a certain icon, and then does a certain function based on the filetype of the particular node. In this case, it allows a sound to be played on a client (I have a separate button that’s for the playing/stopping of music).