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

Listing All Files and Directories Seperately

Started by EtzGamer, 28 June 2016 - 05:54 AM
EtzGamer #1
Posted 28 June 2016 - 07:54 AM
I am trying to list all files and directories on the root directory with fs.list("/") and then seperate them to individual tables.

However, it does seem to be listing the files and directories correctly.

Code:
Spoiler

-- Test file lister

local tFsList = fs.list("/")
local tDirList = {}
local tFileList = {}

local i = 1

for i, file in ipairs(tFsList) do
--for i=1, #tFsList, 1 do
	print(file)
	if fs.isDir(file) then

		table.insert(tDirList, file)

	else

		table.insert(tFileList, file)

	end
end

print("Files:")

local i = 1
for i=1, #tFileList do

	print(tFileList[i])

end

print("Directories:")

local i = 1
for i=1, #tDirList do

	print(tFileList[i])

end
The_Cat #2
Posted 28 June 2016 - 09:50 AM
You made the smallest error :)/>


-- Test file lister
local tFsList = fs.list("/")
local tDirList = {}
local tFileList = {}
--#local i = 1
for i, file in ipairs(tFsList) do
--for i=1, #tFsList, 1 do
	    print(file)
	    if fs.isDir(file) then
			    table.insert(tDirList, file)
	    else
			    table.insert(tFileList, file)
	    end
end
print("Files:")
--#local i = 1
for i=1, #tFileList do
	    print(tFileList[i])
end
print("Directories:")
--#local i = 1 You also don't need to make a variable here, it will get created in the for loop
for i=1, #tDirList do
	    --#print(tFileList[i]) <-- Was this
	    print(tDirList[i]) --# <-- Now this
end
EtzGamer #3
Posted 29 June 2016 - 12:07 PM
You made the smallest error :)/>


-- Test file lister
local tFsList = fs.list("/")
local tDirList = {}
local tFileList = {}
--#local i = 1
for i, file in ipairs(tFsList) do
--for i=1, #tFsList, 1 do
		print(file)
		if fs.isDir(file) then
				table.insert(tDirList, file)
		else
				table.insert(tFileList, file)
		end
end
print("Files:")
--#local i = 1
for i=1, #tFileList do
		print(tFileList[i])
end
print("Directories:")
--#local i = 1 You also don't need to make a variable here, it will get created in the for loop
for i=1, #tDirList do
		--#print(tFileList[i]) <-- Was this
		print(tDirList[i]) --# <-- Now this
end

Eheh.

I couldn't believe I overlooked that minor slip-up on the code

I really do appreciate your help :)/>