Member Avatar
kirtan_thakkar

I have a folder in witch i have a sub-folders and in each sub-folder i have text files(.txt). I want to count that text files and want to print the result on the page.
Can someone code it out???

Hi Kirtan,

What have you tried so far? If you'd like some pointers to start with, look at the functions scandir and pathinfo.

You can then use scandir to recursively find all files in a given directory, and use pathinfo to extract and filter on the file extension.

R.

Member Avatar
kirtan_thakkar

Hi Kirtan,

What have you tried so far? If you'd like some pointers to start with, look at the functions scandir and pathinfo.

You can then use scandir to recursively find all files in a given directory, and use pathinfo to extract and filter on the file extension.

R.

I am not so expert..
Can you code it??

Yes, I can code it. But the point of this forum is that you should learn. But you don't seem willing to do anything to help yourself!

You cannot have even read about the functions I posted links to, because one of the examples actually shows you how to count the number of files in a directory!!

Try yourself first, and then I will help from there...

Member Avatar
kirtan_thakkar

Yes, I can code it. But the point of this forum is that you should learn. But you don't seem willing to do anything to help yourself!

You cannot have even read about the functions I posted links to, because one of the examples actually shows you how to count the number of files in a directory!!

Try yourself first, and then I will help from there...

Thanks..
I have coded for count on a folder..
But I don't know how to code for subfolders too..

Here is the I have tried:

$file=scandir("Folder");
$result=count($file)-2;
echo($result);

It was printing 2 more result . and ..
So I have used -2.

But I have to count only txt files in subfolders..
I have files Like this

Main_Folder -> Folder1 -> 1.txt, 2.txt, 3.txt, ... , 321.txt
Folder2 -> 1.txt, 2.txt, 3.txt, ... , 212.txt
Folder3 -> 1.txt, 2.txt, 3.txt, ... , 857.txt
Folder4 -> 1.txt, 2.txt, 3.txt, ... , 271.txt
Folder5 -> 1.txt, 2.txt, 3.txt, ... , 721.txt
Folder6 -> 1.txt, 2.txt, 3.txt, ... , 445.txt

I have to count those all txt files...

Member Avatar
kirtan_thakkar

I have a list of file in an array..
From It how could I count files witch have these four last words ".txt"....

Now help I have done so many things...

Okay, that's a good start. The scandir function doesn't recursively read directories on its own, so you will need to wrap it into a function so you can initiate the recursion yourself. For example:

$intFileCount = countFilesInDirectory('/var/www/dir1', array('txt'));
echo $intFileCount;

function countFilesInDirectory($strPathToDirectory, $arrExtensions) {
    $intFileCount = 0;
    $arrFiles = scandir($strPathToDirectory);

    foreach($arrFiles as $strFile) {
        if('.' != $strFile && '..' != $strFile) {
            if(is_dir("$strPathToDirectory/$strFile") && is_readable("$strPathToDirectory/$strFile")) {
                $intFileCount += countFilesInDirectory("$strPathToDirectory/$strFile", $arrExtensions);

            } else {
                $arrFileInfo = pathinfo($strFile);
                if(in_array($arrFileInfo['extension'], $arrExtensions)) {
                    $intFileCount += 1;
                }
            }
        }
    }

    return $intFileCount;
}

Have a read through the above, and see if you understand what it is doing.

R

Member Avatar
kirtan_thakkar

Okay, that's a good start. The scandir function doesn't recursively read directories on its own, so you will need to wrap it into a function so you can initiate the recursion yourself. For example:

$intFileCount = countFilesInDirectory('/var/www/dir1', array('txt'));
echo $intFileCount;

function countFilesInDirectory($strPathToDirectory, $arrExtensions) {
    $intFileCount = 0;
    $arrFiles = scandir($strPathToDirectory);

    foreach($arrFiles as $strFile) {
        if('.' != $strFile && '..' != $strFile) {
            if(is_dir("$strPathToDirectory/$strFile") && is_readable("$strPathToDirectory/$strFile")) {
                $intFileCount += countFilesInDirectory("$strPathToDirectory/$strFile", $arrExtensions);

            } else {
                $arrFileInfo = pathinfo($strFile);
                if(in_array($arrFileInfo['extension'], $arrExtensions)) {
                    $intFileCount += 1;
                }
            }
        }
    }

    return $intFileCount;
}

Have a read through the above, and see if you understand what it is doing.

R

Thanks...