Tip: How many contents in a specified folder?

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

Tip: How many contents in a specified folder?

Post by KawaGeo »

Ever wonder how many contents are contained in a given folder on PSP?

Here is how...

Code: Select all

files = System.listDirectory "pathname"
-- files is a list of all files and subdirs in the pathname

local ndirs = 0
local nfiles = 0
for i = 1, table.getn(files) do
  if files[i].directory then
    ndirs = ndirs + 1
  else
    nfiles = nfiles + 1
  end
end    

print("# of subdirs found in the list", ndirs)
print("# of files found in the list", nfiles)
Warning: Don't run the script on Windows. The current version does not support System.listDirectory() yet.

Have fun!

Edited 9/26/2005 8:27PM
Last edited by KawaGeo on Tue Sep 27, 2005 1:30 pm, edited 2 times in total.
Geo Massar
Retired Engineer
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

does that contain folders in folders?

greets
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

Post by KawaGeo »

I haven't included some folders in the folder for testing but I believe they are counted as well.

Thanks for asking.
Geo Massar
Retired Engineer
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

KawaGeo wrote:I haven't included some folders in the folder for testing but I believe they are counted as well.
No, they are not counted, listDirectory is the same like the DOS "dir" command or the Unix "ls" command (without any switches). But it is easy to count all files and directories in a directory and all subdirectories:

Code: Select all

function countFiles(directory)
	local flist = System.listDirectory(directory)
	local filesCount = 0
	local directoriesCount = 0
	for index, file in flist do
		if file.name ~= "." and file.name ~= ".." then
			if file.directory then
				local subdir = directory
				if string.sub(subdir, -1) ~= "/" then
					subdir = subdir .. "/"
				end
				subdir = subdir .. file.name
				newFiles, newDirectories = countFiles(subdir)
				filesCount = filesCount + newFiles
				directoriesCount = directoriesCount + newDirectories + 1
			else
				filesCount = filesCount + 1
			end
		end
	end
	return filesCount, directoriesCount
end

files, directories = countFiles(System.currentDirectory())
print("current directory: files: " .. files .. ", directories: " .. directories)
files, directories = countFiles("ms0:/")
print("memory stick: files: " .. files .. ", directories: " .. directories)
KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

Post by KawaGeo »

I tested the code (after being editted) in the first post and found out that table.getn(files) counts every files and subdirs in a SINGLE folder.

Sorry, dude. I should have said "contents" instead of "files" in the title.
Geo Massar
Retired Engineer
Post Reply