Count files without a loop ?

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post May 10th, 2009, 11:06 am

I'm aware of counting files in a directory using a loop like in this thread.

I was wondering if there were some kind of filesystem indexes I could look at to get the number of files in a directory without actually looping through the list of files though.

I've already thought of counting a scandir or grep. Both seem like just a shortcut so I don't have to do the loop though.
Strong with this one, the sudo is.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post May 10th, 2009, 11:06 am

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post May 10th, 2009, 2:21 pm

Simple way using a shell:
Quote:
$ ls -1 | wc -l


Just add more switches to 'ls' to control what kind of files/folders get included.
The Beer Monocle. Classy.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post May 10th, 2009, 2:55 pm

I'm thinking that would go in the category of scandir and grep.

I'm looking for something kinda raw, I was looking at the Direct IO functions thinking maybe there was something that read an index on a filesystem that keeps special information about a directory.

Kinda like stat() returns details about a file in an array, it would be great if there were a dir_stat() or something that returned details about a directory, like the number of files in it.

Whatever it would read would be updated by the operating system any time a file was added or removed from the directory.

I don't even know if such a thing exists at the OS/filesystem level, let alone if PHP has something to read such a thing. :)
Strong with this one, the sudo is.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post May 10th, 2009, 3:24 pm

Nothing that I know of. Any particular reason you want this to be that efficient?
The Beer Monocle. Classy.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post May 10th, 2009, 3:58 pm

It's one of those I want to use it if it exists things. :)
Strong with this one, the sudo is.
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post May 10th, 2009, 8:02 pm

How does scandir work? It uses a loop as well I suppose...

Are you going to use the files later after you tell how many files there are? If so, you could use sizeof(); of the scandir(); and then use that set result of scandir(); showing the files... using one loop to do two things.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post May 11th, 2009, 4:49 am

scandir returns a list of files, I just need the counts.
Strong with this one, the sudo is.
  • Graham Bell
  • Newbie
  • Newbie
  • No Avatar
  • Joined: May 08, 2009
  • Posts: 12
  • Status: Offline

Post May 11th, 2009, 4:55 am

Well. I would get this information from database. I believe that is one row script
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post May 11th, 2009, 5:03 am

I'll be getting counts from one of the database tables, but since this is for the management area, checking the count from the database against the actual filesystems count comes in handy.
Strong with this one, the sudo is.
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post May 11th, 2009, 2:20 pm

joebert wrote:
scandir returns a list of files, I just need the counts.

I was talking something about:
PHP Code: [ Select ]
<?php
$dir = (isset($_GET['dir'])) ? $_GET['dir'] : './';
$file_list = scandir($dir);
$file_count = sizeof($file_list);
 
echo "<p>You got {$file_count} files in {$dir}</p>\n";
foreach($file_list as $file)
{
   echo "{$file} <br />\n";
}
?>
  1. <?php
  2. $dir = (isset($_GET['dir'])) ? $_GET['dir'] : './';
  3. $file_list = scandir($dir);
  4. $file_count = sizeof($file_list);
  5.  
  6. echo "<p>You got {$file_count} files in {$dir}</p>\n";
  7. foreach($file_list as $file)
  8. {
  9.    echo "{$file} <br />\n";
  10. }
  11. ?>

Only downfall of this, is that it counts the directories as well.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post May 11th, 2009, 2:45 pm

I know. :)

Imagine that someone wanted 1000 sammiches. Using scandir would be like going into the back room and counting every single ingredient manually to determine if we have enough of everything to make 1000 sammiches.

Now imagine if someone had been taking inventory all day long and there was a list of how many ingredients we have left in the back room.

I'm looking for a way to quickly look at that list which has been getting updated periodicly throughout the day instead of going through and counting everything in the back room. :)
Strong with this one, the sudo is.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post May 11th, 2009, 2:52 pm

joebert, if I ever get a chance to meet you, remind me to pack sammiches - they seem to be on ur mind a lot :)
I'd love to change the world, but they won't give me the source code.
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post May 11th, 2009, 3:05 pm

I don't know... how does this look? I know it's not exactly what you are looking for...

Actually, now that I read the description, I don't really think it's what you want, but if you ever need something like it, then this is probably a good thing.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8

Post Information

  • Total Posts in this topic: 13 posts
  • Users browsing this forum: Kurthead+1 and 228 guests
  • 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 post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.