0

I am trying to figure out how many sub-folders contain a folder called "img", so I want to implement a recursive count of all subfolders called "img", if this is possible.

Also, how can I do a count that is one level deep? i.e it will look only one folder deep for all folders called "img"?

So, if my folder structure was :

>folder1
  >img
>folder2
  >subfolder
    >img
>folder3
   >subfolder
      >subsubfolder
        >img
>folder4
  >img

The first part would return 4, and the second script would return 2.

Is this possible to do either from CMD or with some Windows tool?

I've had a look around and the closest I could find was a CLI script to print all folders and sub-folders to a file :

dir /directory:n /ad > f.txt

from here.

Which I could then copy paste into Notepad++ and do some manual messing around with...but I was hoping there was a cleaner/faster solution than this in case I ever come across this in the future. What would I need to write for this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Simon Kiely
  • 5,880
  • 28
  • 94
  • 180

2 Answers2

1
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
SET /a onelevelcount=0
FOR /f "delims=" %%a IN (
 'dir /b /ad "%sourcedir%" 2^>nul'
 ) DO (
 IF EXIST "%sourcedir%\%%a\img\." SET /a onelevelcount+=1
)
ECHO one level down=%onelevelcount%
SET /a grandtotal=0
FOR /f %%c IN ('dir /s /ad "%sourcedir%"  2^>nul^|findstr /i /e /r "\\img"') DO SET /a grandtotal+=1
ECHO grand total=%grandtotal%

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

As for how you'd figure it out - practice makes perfect. Lots of examples on SO.

Magoo
  • 77,302
  • 8
  • 62
  • 84
-2

type the folowing code on notepad and save it as .dll

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
SET /a onelevelcount=0
FOR /f "delims=" %%a IN (
 'dir /b /ad "%sourcedir%" 2^>nul'
 ) DO (
 IF EXIST "%sourcedir%\%%a\img\." SET /a onelevelcount+=1
)
ECHO one level down=%onelevelcount%
SET /a grandtotal=0
FOR /f %%c IN ('dir /s /ad "%sourcedir%"  2^>nul^|findstr /i /e /r "\\img"') DO SET /a grandtotal+=1
ECHO grand total=%grandtotal%

GOTO :EOF`enter code here`