How to Count All Files Inside a Folder, Its Subfolder and All . the Count Should Not Include Folder Count

How to count all files inside a folder, its subfolder and all . The count should not include folder count

find . -type f | wc -l will recursively list all the files (-type f restricts to only files) in the current directory (replace . with your path). The output of this is piped into wc -l which will count the number of lines.

Python count files in a directory and all its subdirectories

IIUC, you can just do

sum(len(files) for _, _, files in os.walk('path/to/folder'))

or perhaps, to avoid the len for probably slightly better performance:

sum(1 for _, _, files in os.walk('folder_test') for f in files)

Batch count all files but exclude those in a specific folder

Use DIR /S /B /A-D to iterate all files. The output includes the full path to each file.

Pipe that result to FINDSTR /L /I /V "\\error\\ to filter out paths that contain \error\. This will also exclude any folders within an error folder. The search could be modified to exclude only 'error' but include children of 'error'.

Pipe that result to FIND /C /V "" to count the number of files (lines).

dir /s /b /a-d | findstr /liv "\\error\\" | find /c /v ""

The above will simply display the count.

If you want to capture the count in a variable, then use FOR /F to parse the output of the above command. Unquoted poison characters like | must be escaped when used in FOR /F.

@echo off
for /f %%N in ('dir /s /b /a-d^|findstr /liv "\\error\\"^|find /c /v ""') do set count=%%N
echo count=%count%

How to show the amount of files in the sub-folders?

Well, you can loop over each subdirectory in ./A and output the number of files/folders contained in each subdirectory with:

for i in A/*; do [ -d "$i" ] && echo "${i##*/}  $(ls -1 "$i" | wc -l)"; done

Which just loops over all files and folders in A, and if the current name is a directory, it echos the directory name and a count of the number of files/folders in that directory.

(note: that is a ls -"one" and a wc -"ell")

To include hidden files in the subdirectories use ls -a1 which is -"a"one" and then subtract 2 from each total (for . and ..)

Give that a try and let me know if you have further questions.

How to count the number of files in a sub folder with a specific name using C#?


    public static void CountFiles(string path)
{
int xFileCount = 0;
int yFileCount = 0;
int zFileCount = 0;

var files = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories);

foreach(string file in files)
{
string folder = new DirectoryInfo(Path.GetDirectoryName(file)).Name;
if (folder == "FOLDER_X")
xFileCount++;
if (folder == "FOLDER_Y")
yFileCount++;
if (folder == "FOLDER_Z")
zFileCount++;
}

Console.WriteLine("X Files : {0}", xFileCount);
Console.WriteLine("Y Files : {0}", yFileCount);
Console.WriteLine("Z Files : {0}", zFileCount);
}

Few tips:

  • If you want to search for a specific type of files (say, for example, only text files) then you can pass search pattern to Directory.GetFiles(), such as ".txt" instead of ".*".
  • If you want to make this more generic, instead of just hardcoding your folder names you could pass that as a parameter.

So I would really use this function, and call it with whatever folder name you want:

    public static int CountFiles(string path, string folderToSearch)
{
int fileCount = 0;

var files = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories);

foreach (string file in files)
{
string folder = new DirectoryInfo(Path.GetDirectoryName(file)).Name;
if (folder == folderToSearch)
fileCount++;
}

return fileCount;
}

Then call it like so:

    static void Main(string[] args)
{
int xFiles = CountFiles("path goes here", "FOLDER_X");
int yFiles = CountFiles("path goes here", "FOLDER_Y");
int zFiles = CountFiles("path goes here", "FOLDER_Z");
}

EDIT: Made a small change to how you get the immediate directory name.

EDIT2: Edited to incorporate the suggestion by @MickyD.

EDIT3: This would pass your most recent requirement.

    public static int CountFiles2(string path, string folderToSearch)
{
int fileCount = 0;
var dirs = Directory.EnumerateDirectories(path, folderToSearch, SearchOption.AllDirectories).ToList();

foreach (string dir in dirs)
{
var files = Directory.EnumerateFiles(dir, "*.*", SearchOption.AllDirectories);
if (files != null)
fileCount += files.Count();
}

return fileCount;
}

count number of subdirectories within subdirectories

You need to use some kind of recursion for that task. What about a sub-routine that loops through the sub-directories and calls itself for each one? What I mean is the following:

@echo off
rem // Define constants here:
set "_PATH=%~1" & rem // (path of the root directory to process)
rem // Define global variables here:
set /A "$DEPTH=0" & rem // (variable to determine the greatest depth)

rem // Initialise variables:
set /A "DEEP=0" & rem // (depth of the current directory branch)
rem // Call recursive sub-routine, avoid empty argument:
if defined _PATH (call :SUB "%_PATH%") else (call :SUB ".")
rem // Return found depth:
echo %$DEPTH%
exit /B

:SUB <root_path>
rem // Loop through all sub-directories of the given one:
for /D %%D in ("%~1\*") do (
rem // For each sub-directory increment depth counter:
set /A "DEEP+=1"
rem // For each sub-directory recursively call the sub-routine:
call :SUB "%%~fD"
)
rem // Check whether current branch has the deepest directory hierarchy:
if %$DEPTH% lss %DEEP% set /A "$DEPTH=DEEP"
rem // Decrement depth counter before returning from sub-routine:
set /A "DEEP-=1"
exit /B

Just as an alternative idea, but with a bit worse performance, you could also determine the number of backslashes (\) in the resolved paths of all sub-directories, retrieve the greatest number and subtract that number of the root directory from the greatest one, like this:

@echo off
rem // Define constants here:
set "_PATH=%~1" & rem // (path of the root directory to process)
rem // Define global variables here:
set /A "$DEPTH=0" & rem // (variable to determine the greatest depth)

rem // Change to root directory:
pushd "%_PATH%" || exit /B 1
rem // Resolve root directory:
call :SUB "."
rem // Store total depth of root directory:
set /A "CROOT=$DEPTH, $DEPTH=0"
rem // Process all sub-directories recursicely:
for /D /R %%D in ("*") do (
rem // Determine greatest depth relative to root:
call :SUB "%%~fD" -%CROOT%
)
rem // Change back to original directory:
popd
rem // Return found depth:
echo %$DEPTH%
exit /B

:SUB <val_path> [<val_offset>]
rem // Resolve provided sub-directory:
set "ITEM=%~f1" & if not defined ITEM set "ITEM=."
rem // Initialise variables, apply count offset:
set "COUNT=%~2" & set /A "COUNT+=0"
rem // Count number of backslashes in provided path:
for %%C in ("%ITEM:\=" "%") do (
set /A "COUNT+=1"
)
rem // Check whether current branch has the deepest directory hierarchy:
if %$DEPTH% lss %COUNT% set /A "$DEPTH=COUNT"
exit /B


Related Topics



Leave a reply



Submit