forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Tip: How many contents in a specified folder?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Tue Sep 27, 2005 5:44 am    Post subject: Tip: How many contents in a specified folder? Reply with quote

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

Here is how...
Code:
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
_________________
Geo Massar
Retired Engineer


Last edited by KawaGeo on Tue Sep 27, 2005 1:30 pm; edited 2 times in total
Back to top
View user's profile Send private message Send e-mail
LuMo



Joined: 21 Aug 2005
Posts: 410
Location: Austria

PostPosted: Tue Sep 27, 2005 6:09 am    Post subject: Reply with quote

does that contain folders in folders?

greets
_________________
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
Back to top
View user's profile Send private message Visit poster's website
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Tue Sep 27, 2005 6:40 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Send e-mail
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Tue Sep 27, 2005 7:46 am    Post subject: Reply with quote

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:

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)
Back to top
View user's profile Send private message
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Tue Sep 27, 2005 1:28 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group