473,373 Members | 1,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,373 software developers and data experts.

count files in a directory

Hello, Using PHP 4, is there a fucntion that counts the files in a
given directory? You can assume there are no sub-directories within
this directory and hence, no files within the sub-directories.

Thanks for any functions or one-liners. - Dave

Jul 23 '06 #1
4 30270
la***********@zipmail.com schreef:
Hello, Using PHP 4, is there a fucntion that counts the files in a
given directory? You can assume there are no sub-directories within
this directory and hence, no files within the sub-directories.

Thanks for any functions or one-liners. - Dave
Hi Dave,

You could use readdir to accomplish this.
Example:

<?php
function countFiles($strDirName)
{
if ($hndDir = opendir($strDirName))
{
$intCount = 0;
while (false !== ($strFilename = readdir($hndDir)))
{
if ($strFilename != "." && $strFilename != "..")
{
$intCount++;
}
}
closedir($hndDir);
}
else
{
$intCount = -1;
}

return $intCount;
}

echo countFiles("C:\\Temp");
?>

HTH.
Peter.

--
http://www.phpforums.nl
Jul 23 '06 #2
<comp.lang.php>
<la***********@zipmail.com>
<23 Jul 2006 06:48:02 -0700>
<11*********************@75g2000cwc.googlegroups.c om>
Hello, Using PHP 4, is there a fucntion that counts the files in a
given directory? You can assume there are no sub-directories within
this directory and hence, no files within the sub-directories.

Thanks for any functions or one-liners.
<?php

$dirname="demo";
$dh=opendir($dirname);
while ($dave=readdir($dh))
{
print "$dave <br>";
}
closedir ($dh);

?>
--
Encrypted email address
www.emailuser.co.uk/?name=KRUSTOV
Make a shorter url
www.vhit.co.uk
Jul 23 '06 #3
<comp.lang.php>
<Krustov>
<Sun, 23 Jul 2006 15:54:51 +0100>
<MP***********************@news.newsreader.com>
<?php

$dirname="demo";
$dh=opendir($dirname);
while ($dave=readdir($dh))
{
print "$dave <br>";
}
closedir ($dh);

?>
I missed the bit where you said count .

<?php

$cnt=0;
$dirname="demo";
$dh = opendir($dirname);
while ($dave=readdir($dh))
{
print "$dave <br>";
$cnt=$cnt+1;
}
closedir ($dh);

print $cnt-2;

?>
--
Encrypted email address
www.emailuser.co.uk/?name=KRUSTOV
Make a shorter url
www.vhit.co.uk
Jul 23 '06 #4

la***********@zipmail.com wrote:
Hello, Using PHP 4, is there a fucntion that counts the files in a
given directory? You can assume there are no sub-directories within
this directory and hence, no files within the sub-directories.

Thanks for any functions or one-liners. - Dave
count(glob("$dir/*.*"));

Jul 23 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: rbt | last post by:
I assume that there's a better way than this to count the files in a directory recursively. Is there??? def count_em(valid_path): x = 0 for root, dirs, files in os.walk(valid_path): for f in...
2
by: Darren | last post by:
Hi Group, Any ideas how to check for files in a directory, to see if any are of the same prefix, and make a count of them. I have a round 100 files, the filename is made up of an engineer...
9
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
3
by: Dale | last post by:
Is there a way to determine the number of hidden files/directories within a directory? This is what I’m doing now: System.IO.DirectoryInfo rootPath; System.IO.FileSystemInfo dirs, files; int...
2
by: Bruce | last post by:
Is there a mechanism within VS 2005 to determine the number of source code lines in a C# project (either per file or across the entire project)? Thanks, Bruce
3
by: glub glub | last post by:
i'm trying to make a program that works as Replace works in MS Word but this is for use with files, not a text document. FolderBrowserDialog1.ShowDialog() txtPath.Text =...
2
by: MichiMichi | last post by:
My asp.net application has a directory which is filled up with over 17 files (email) a second. After a while this sums up into a huge amount of files. I usually count files with the GETFILES...
4
by: G E W hittle | last post by:
I have a directory of tiff files. I need to loop thru the files and count the number of pages in each file. Can someone point me to where this info is stored? GW
6
by: flavourofbru | last post by:
Hi, I have a small question. How do I compute the number of files(which are specific such as .doc) in a directory in C++. For example, if I have 10 files of type .doc format in a directory,...
1
by: briggs | last post by:
Hi All, I need to take the count of txt and bak files present in all the directory.for eg if we assume there are two directoies say(test1, test2), I need to collect the count(of txt and bak files)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.