This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
DannySMc's profile picture

Code can't distinguish files and folders in any other path other than root?

Started by DannySMc, 14 January 2015 - 02:00 PM
DannySMc #1
Posted 14 January 2015 - 03:00 PM
I am attempting to make my own GUI based file manager and it all works other than one problem, I have some code that will automatically distinguish a folder and a file, but for some reason it only registers folders in the "/" root folder and no other folder so for example, If I get the contents of the "/" folder I will get:

+ .Lazers_Data
+ core
+ rom
- FileFinder.lua
- startup
- vde.lua

the plus (+) being a folder and the minus (-) being a file… but when I attempt to do the following directory: "/core/" I get:

- addons
- apps
- config
- libs
- test
- themes
- users

All of them are folders?!

Here is the code for the directory/file grabber:

function fb_getDir(sDir)
local newList = {}
tFilesClean = {}
local listFile = fs.list(sDir)
for _,v in ipairs(listFile) do
   if fs.isDir(v) then
	  table.insert(newList, "+ "..v)
	  print(v)
	  table.insert(tFilesClean, v)
   end
end
for _,v in ipairs(listFile) do
   if not fs.isDir(v) then
	  table.insert(newList, "- "..v)
	  table.insert(tFilesClean, v)
   end
end
   return newList
end

Thanks
Edited on 14 January 2015 - 02:00 PM
Lignum #2
Posted 14 January 2015 - 03:04 PM
fs.list returns files relative to the directory specified.
You need to do this:

if fs.isDir(fs.combine(sDir, v)) then
DannySMc #3
Posted 14 January 2015 - 03:10 PM
fs.list returns files relative to the directory specified.
You need to do this:

if fs.isDir(fs.combine(sDir, v)) then

The directory is given when it does the fs.list()? and the code didn't work it just made my code print it twice, one set being folders and the other being files…?
wieselkatze #4
Posted 14 January 2015 - 03:10 PM
fs.list() does not return the whole path of the file/folder.
Try this instead:


local function fb_getDir( sDir )
    sDir = ( "/" .. sDir .. "/" ):gsub( "\\+", "/" ):gsub( "/+", "/" )
    local folder, list = sDir:match( ".*/" ), {}

    if not fs.isDir( sDir ) then
	    return {}
    end

    for k, v in pairs( fs.list( sDir ) ) do
	    if fs.isDir( folder .. v ) then
		    list[ #list+1 ] = "+ " .. v
	    else
		    list[ #list+1 ] = "- " .. v
	    end
    end

    return list
end



This also automatically formats the string sDir so it won't crash if you accidentally use two dashes or sth. like that.
Edited on 14 January 2015 - 02:13 PM
DannySMc #5
Posted 14 January 2015 - 03:34 PM
fs.list() does not return the whole path of the file/folder.
Try this instead:


local function fb_getDir( sDir )
	sDir = ( "/" .. sDir .. "/" ):gsub( "\\+", "/" ):gsub( "/+", "/" )
	local folder, list = sDir:match( ".*/" ), {}

	if not fs.isDir( sDir ) then
		return {}
	end

	for k, v in pairs( fs.list( sDir ) ) do
		if fs.isDir( folder .. v ) then
			list[ #list+1 ] = "+ " .. v
		else
			list[ #list+1 ] = "- " .. v
		end
	end

	return list
end



This also automatically formats the string sDir so it won't crash if you accidentally use two dashes or sth. like that.

This works, but is there a way of keeping all the folders at the top instead of scattered? Thanks!
wieselkatze #6
Posted 14 January 2015 - 04:05 PM
How about


local function fb_getDir( sDir )
    sDir = ( "/" .. sDir .. "/" ):gsub( "\\+", "/" ):gsub( "/+", "/" )
    local folder, list = sDir:match( ".*/" ), {}

    if not fs.isDir( sDir ) then
	    return {}
    end

    for k, v in pairs( fs.list( sDir ) ) do
	    if fs.isDir( folder .. v ) then
		    table.insert( list, 1, "+ " .. v )
	    else
		    list[ #list+1 ] = "- " .. v
	    end
    end

    return list
end

That should've been pretty easy to solve. See here.
Edited on 14 January 2015 - 03:06 PM
DannySMc #7
Posted 14 January 2015 - 04:10 PM
How about


local function fb_getDir( sDir )
	sDir = ( "/" .. sDir .. "/" ):gsub( "\\+", "/" ):gsub( "/+", "/" )
	local folder, list = sDir:match( ".*/" ), {}

	if not fs.isDir( sDir ) then
		return {}
	end

	for k, v in pairs( fs.list( sDir ) ) do
		if fs.isDir( folder .. v ) then
			table.insert( list, 1, "+ " .. v )
		else
			list[ #list+1 ] = "- " .. v
		end
	end

	return list
end

That should've been pretty easy to solve. See here.

Works perfectly thank you! :D/> +1
Bomb Bloke #8
Posted 14 January 2015 - 10:30 PM
Bear in mind that fs.list() returns in an order determined by the underlying file system. This may or may not be alphabetical, depending on the platform the MineCraft server is running on.
DannySMc #9
Posted 14 January 2015 - 10:32 PM
Bear in mind that fs.list() returns in an order determined by the underlying file system. This may or may not be alphabetical, depending on the platform the MineCraft server is running on.

I already know this? hence the first code I uploaded just placed all the folders above the files? I don't care if it's alphabetical?:S