0

I have the directory structure like this A->B->C,D->E,F Where A contains B folder and some text files,sql files,log files etc. B contains C folder and many other extension files. Similarly E,F are also sub-directories and contains some files. Now the output should be something like this: Count of files: A-10 A/B-15 A/B/C-20 D-25 D/E-30 F-12 Thanks for your response.All approaches are appreciated.

Have tried:

@ECHO OFF 
SET "rootpath=%~1" 
FOR /D %%D IN ("%~dp0*") 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! 
    ENDLOCAL 
    pause
)
Vika Marquez
  • 353
  • 1
  • 3
  • 12
  • 1
    What do you got so far? What have you tried? – MadsTheMan Feb 02 '16 at 06:53
  • I tried the below code but its pulling wrong results and giving only the immediate folders but not the count of files in sub-directories. @ECHO OFF SET "rootpath=%~1" FOR /D %%D IN ("%~dp0\*") 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! ENDLOCAL pause ) Thanks for your help!! – krishnu morla Feb 02 '16 at 07:16
  • Your description and question title are unclear; so you want to get the count of _files_ (not dir.s) per (sub-)directory in the full tree in the current directory? does the tree root contain files to be counted too? is `F` a dir. on top-level or is it a sub-dir. of `E`? does the output need to show relative paths to the (sub-)directories? And what is the problem with your code? – aschipfl Feb 02 '16 at 10:29
  • I want ot get the count of files in directories,sub-directories and sub-sub directories.So I want the count if F and E also.The problem with my code was that it was pulling the results(count of files) for immediate folders where I triggered the .bat file and its not pulling the count of files – krishnu morla Feb 02 '16 at 11:57

1 Answers1

1

You need a recursive subroutine that traverse the entire tree and perform the same calculation in each subfolder. This approach is simpler:

EDIT: Code modified to avoid errors when subfolder names have spaces.

@echo off
setlocal EnableDelayedExpansion

set "base=%cd%\"

rem Accumulate files in the same array element given by its folder
for /R %%a in (*.*) do (
   set "folder=%%~DPa"
   set "folder=!folder:%base%=!"
   set /A "count[!folder: =_!]+=1" 2> nul
)

rem Show the result
for /F "tokens=2,3 delims=[]=" %%a in ('set count[') do (
   echo %%a-%%b
)

For further description of array management in Batch files, see: Arrays, linked lists and other data structures in cmd.exe (batch) script

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Could you please tell me how to output the path and no of files: A-10 A/B-20 something like this..please don't feel bad I am not good at batch script. Thanks for your quick reponse – krishnu morla Feb 02 '16 at 17:47
  • While I m running this bat I could able to see some error 'missing operand' will this be causing an issue.please help – krishnu morla Feb 04 '16 at 05:02
  • The problem was caused by folder names that included spaces. I modified the code to replace spaces by underscores. If you wish, the spaces may be recovered before the result is displayed... – Aacini Feb 04 '16 at 06:03
  • I am very grateful to you...Thanks a lot!! – krishnu morla Feb 04 '16 at 07:10