Rick FlyFish
Rick FlyFish

Reputation: 105

Batch file to count files in a in subdirectories

I would like to get this batch file to count the files in the subdirectories and display the total files and directories. However, I'm not familiar with the syntax.

@ECHO OFF
SET "rootpath=C:\Users\RX\Documents\01.00 Meters\100\EMC\EMC 15Aug2016 Level4\res"
SET tcnt=0
FOR /D %%D IN ("%rootpath%\*") DO (
  SET cnt=0
  FOR /F %%K IN ('DIR /A-D /S "%%D" 2^>NUL ^| FIND "File(s)" ^|^| ECHO 0') DO (
    SET /A cnt+=%%K
  )
  SETLOCAL EnableDelayedExpansion
  ECHO %%D: !cnt!
  tcnt+=%%cnt
  ENDLOCAL
)
ECHO !tcnt!
cmd /k

Upvotes: 1

Views: 5101

Answers (3)

CristiFati
CristiFati

Reputation: 41116

Here's an approach that uses recursion.

It comes from programming rather than sys admining; it uses a function (the batch equivalent is a label) that takes a folder as an argument, sums the file number in that folder to a global variable, and then calls itself for each sub-folder (with each call increments the folders number).

@echo off
setlocal enableextensions, enabledelayedexpansion

set _FOLDER="C:\Temp"
set /a _FILES=0
set /a _DIRS=0

call :handle_dir %_FOLDER%
echo Dirs: %_DIRS%, Files: %_FILES%
goto :eof

:handle_dir
    set /a _DIRS=%_DIRS%+1
    for /f %%f in ('dir /b /a:-d "%~1" 2^>nul') do (
        set /a _FILES=!_FILES!+1
    )
    for /f %%f in ('dir /b /a:d "%~1"') do (
        call :handle_dir "%~1\%%f"
    )
    goto :eof

Simply set the _FOLDER variable to the folder that you want its files and subdirs counted.

Note: On a folder containing tons of files and subfolders, it might yield StackOverflow :) .

Upvotes: 1

Compo
Compo

Reputation: 38589

This should provide just the totals:

@Echo Off
SetLocal EnableDelayedExpansion

(Set selDir=C:\Users\RX\Documents\01.00 Meters\100\EMC\EMC 15Aug2016 Level4\res)

Set i=3
For /F "EOL=- Tokens=1-3" %%I In (
    'RoboCopy /L /E /NFL /NDL /NJH "%selDir%" null *.*') Do (
    If %%J==: (Set/A i-=1
        If !i! GEq 1 Echo(%%I : %%K))
Pause

This should give an output showing the relative directory names followed by their respective file counts.

@Echo Off
SetLocal

(Set selDir=C:\Users\RX\Documents\01.00 Meters\100\EMC\EMC 15Aug2016 Level4\res)

Set "i=0"
For /F "Delims=" %%I In ('Dir/B/AD/S "%selDir%"') Do (Set/A i+=1
    Set "$d=%%I"
    Call Set "$d[%%i%%]=%%$d:%seldir%\=%%"
)
Echo(
For /F "Tokens=1* Delims==" %%I In ('Set $d[') Do (Set "i=0"
    For /F "Delims=" %%K In ('Dir/B/A-D "%selDir%\%%J\*.*"2^>Nul') Do Set/A i+=1
    Call Echo( %%J : %%i%%) 
Echo(
Pause
EndLocal
Exit/B

Upvotes: 1

jan-seins
jan-seins

Reputation: 1283

This counts all files and all directories recursivly starting at rootpath

@echo off
SET rootpath=c:\temp
set cnt_files=0
set cnt_dirs=0
rem count all files in dir and subdirs
for /f %%a in ('dir /s /B /a-d "%rootpath%"') do set /A cnt_files+=1
rem count all folders in dir and subdirs
for /f %%a in ('dir /s /B /ad "%rootpath%"') do set /A cnt_dirs+=1
echo files:%cnt_files%
echo dirs:%cnt_dirs%
pause

The output looks s.th. like this:

files:4051
dirs:1559

I hope this does it for you.

Upvotes: 2

Related Questions