Count files without a loop ?

  • joebert
  • Sledgehammer
  • Genius
  • User avatar
  • Posts: 13496
  • Loc: Florida

Post 3+ Months Ago

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.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6229
  • Loc: Seattle, WA

Post 3+ Months Ago

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.
  • joebert
  • Sledgehammer
  • Genius
  • User avatar
  • Posts: 13496
  • Loc: Florida

Post 3+ Months Ago

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. :)
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6229
  • Loc: Seattle, WA

Post 3+ Months Ago

Nothing that I know of. Any particular reason you want this to be that efficient?
  • joebert
  • Sledgehammer
  • Genius
  • User avatar
  • Posts: 13496
  • Loc: Florida

Post 3+ Months Ago

It's one of those I want to use it if it exists things. :)
  • Bogey
  • Bogey
  • Genius
  • Bogey
  • Posts: 8363
  • Loc: USA

Post 3+ Months Ago

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.
  • joebert
  • Sledgehammer
  • Genius
  • User avatar
  • Posts: 13496
  • Loc: Florida

Post 3+ Months Ago

scandir returns a list of files, I just need the counts.
  • Graham Bell
  • Newbie
  • Newbie
  • Graham Bell
  • Posts: 12

Post 3+ Months Ago

Well. I would get this information from database. I believe that is one row script
  • joebert
  • Sledgehammer
  • Genius
  • User avatar
  • Posts: 13496
  • Loc: Florida

Post 3+ Months Ago

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.
  • Bogey
  • Bogey
  • Genius
  • Bogey
  • Posts: 8363
  • Loc: USA

Post 3+ Months Ago

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.
  • joebert
  • Sledgehammer
  • Genius
  • User avatar
  • Posts: 13496
  • Loc: Florida

Post 3+ Months Ago

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. :)
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

joebert, if I ever get a chance to meet you, remind me to pack sammiches - they seem to be on ur mind a lot :)
  • Bogey
  • Bogey
  • Genius
  • Bogey
  • Posts: 8363
  • Loc: USA

Post 3+ Months Ago

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.

Post Information

  • Total Posts in this topic: 13 posts
  • Users browsing this forum: No registered users and 81 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
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.