Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the count of sub-folders

Tags:

php

count

glob

I was using count(glob("test/*")) to count the sub-folders in the test folder, but now that I also have files in the test folder, not just folders, I get incorrect results. Is there a way to modify the glob pattern so that it'll return only the folders, not files?

I've thought about a workaround. Get the total count of folders and files, get the count of files only, and then, subtract the count of files from the count of the whole.

$total_items  = count(glob("test/*"));
$total_files  = count(glob("test/*.*"));
$folder_count = $total_items - $total_files;

This works, but there might be a simpler way to do this.

like image 354
akinuri Avatar asked Jan 29 '16 22:01

akinuri


People also ask

How do I count files in a subfolder?

Right-click on the folder and select the “Properties” option. The “Properties” window will open and you will be able to see the number of files and subdirectories located in the directory selected.

How do I find the number of subdirectories in a directory?

Using the ls Command. The ls command lists the directories and files contained in a directory. The ls command with the -lR options displays the list (in long format) of the sub-directories in the current directory recursively. Then, we use the grep command to search for a string or pattern in the input.

How do I count subfolders in Outlook?

Choose File-> section Info-> button Cleanup Tools-> Mailbox Cleanup…Now click on [View Mailbox Size…] and you'll be presented with a list of your folders and subfolders. You'll need to count the folders yourself, as they are not counted by the system, but their size is listed.


1 Answers

You have to use the option GLOB_ONLYDIR to return only directories:

$total_items  = count( glob("test/*", GLOB_ONLYDIR) );
like image 158
fusion3k Avatar answered Sep 26 '22 03:09

fusion3k