| View previous topic :: View next topic |
| Author |
Message |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Tue Sep 27, 2005 5:44 am Post subject: Tip: How many contents in a specified folder? |
|
|
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 |
|
 |
LuMo
Joined: 21 Aug 2005 Posts: 410 Location: Austria
|
Posted: Tue Sep 27, 2005 6:09 am Post subject: |
|
|
does that contain folders in folders?
greets _________________ "Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com |
|
| Back to top |
|
 |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Tue Sep 27, 2005 6:40 am Post subject: |
|
|
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 |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Tue Sep 27, 2005 7:46 am Post subject: |
|
|
| 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 |
|
 |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Tue Sep 27, 2005 1:28 pm Post subject: |
|
|
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 |
|
 |
|