-   Programming (
-   -   Determine if folder has subfolders with out overhead from find or ls (

davstin2002 12-20-2006 05:11 PM

Determine if folder has subfolders with out overhead from find or ls
I am writing a program with a folder tree using php.

I want to determine if a folder has subfolders or not for the folder tree so i can use plus/minus for expand.

For example, assume i'm looking at the following folder structure

where /usr/local and /var are expanded & everything else is collapsed. I want to avoid having a +/- sign if a folder has no subfolders. For example, my /usr/local/bin and /usr/local/sbin don't have subfolders so I would rather it look like this:


The graphics side of things i can handle. The part i'm having a problem with is the determining of subfolders. The command I'm using right now to determine is this:
`find /some/path -type d -maxdepth 1`
However I've got some folders with a bunch of stuff in them. So I get a lot of overhead as it searches through unnecessary stuff.

I've tried something similar with ls but get a lot of overhead as well.

I've searched google but didnt find anything useful.

Any help would be appreciated.



trickykid 12-20-2006 05:55 PM

I don't think there's much else you could use. You could try something perhaps like this:

ls -l | grep "^d"

And perhaps use awk to get the directories listed without the other junk, unless you have another way to get it.

reddazz 12-20-2006 06:48 PM

Moved: This thread is more suitable in the Programming forum and has been moved accordingly to help your thread/question get the exposure it deserves.

hvc2 12-20-2006 07:36 PM

See "stat -c=%f *" Look at info stat and man stat. Switch between -c=%f and -c=%F to get a feel for its output.

crabboy 12-20-2006 10:30 PM

I don't get it. If you are using php then why make calls into the shell for the directory info. Use the php methods:

davstin2002 12-21-2006 09:51 AM

well it's because of permission reasons. I'm really kinda cheating with sudo to do stuff. I don't want to give the webserver it's self a ton of access to the server, but I'd like to be able to restrict it so sudo made the most sense to me. opendir doesn't give you that option unless I'm missing something. Any thoughts on that would be appreciated.

davstin2002 12-21-2006 12:58 PM

i haven't been able to find much of a solution.

I even tried writing an external perl script with the following code to exit out as soon as it finds a subfolder.
opendir (DIR, $ARGV[0]) || die "Can't open directory $ARGV[0]\n";
$ct = 0;
while ($next = readdir(DIR)) {
$ct++ if (-d $ARGV[0]."/".$next);
if ($ct > 2) { print "true\n"; exit;}
closedir (DIR);
print "false\n";

It works fine, but the problem is it still has quite a bit of overhead, although is slightly quicker.

For the time being I'm just going to allow +/- signs on folders w/o subfolders but would like to fix that later.

demon_vox 12-21-2006 01:35 PM

You can use glob with the option GLOB_ONLYDIR which will get you an array with just the directory names of a given directory. Of course that if the array is empty, there are no directory :)


davstin2002 12-21-2006 03:26 PM

i'll look into that. for now i will keep it expandable for all directories. it's not super critical to the functionality of the program so i'll hold off on it

All times are GMT -5. The time now is 10:26 AM.