Get the total size of all subdirectories with a specific name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
terryhenderson
Posts: 16
Joined: 22 Jul 2018 04:59

Get the total size of all subdirectories with a specific name

#1 Post by terryhenderson » 28 Sep 2020 04:03

I have a long and deep tree which contains numerous subdirectories for many levels down.
"resfiles" is one of those subdirectories that exists in various levels of that tree.
What I need is a cmd or a batch that sums the total size of all resfiles subdirectories in all levels and returns:
1. the number of resfiles subdirectories
2. the total size of all those resfiles subdirectories of that tree.


Can I get some precious assistance with this issue, please?

Many thanks in advance.

T.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Get the total size of all subdirectories with a specific name

#2 Post by aGerman » 28 Sep 2020 10:59

There are some threads about the size of a directory here at DosTips, like that one:
viewtopic.php?f=3&t=9615&p=62024

Although I still have my doubts that Batch is the right language for a task like this. Even if you are able to get the total size, keep in mind that math in Batch is limited to 32 bit integers.

Steffen

terryhenderson
Posts: 16
Joined: 22 Jul 2018 04:59

Re: Get the total size of all subdirectories with a specific name

#3 Post by terryhenderson » 29 Sep 2020 04:38

Thank you very much indeed Steffen for your reply.
I believe the referred to link does have the solution for my question. I just need to specifically refer to any "resfiles" subfolder in this code:

Code: Select all

@echo off &setlocal
set "folder=%userprofile%\Desktop"
for /f "tokens=1,2 delims=: " %%a in ('robocopy "%folder%" "%folder%" /L /S /NJH /BYTES /FP /NC /NDL /NFL /TS /XJ /R:0 /W:0') do if /i "%%a"=="Bytes" set "size=%%b"
echo %size% bytes
pause
how this can be done in:

Code: Select all

set "folder=%userprofile%\Desktop"
I really appreciate any advice.

Many thanks ..

T.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Get the total size of all subdirectories with a specific name

#4 Post by aGerman » 29 Sep 2020 10:44

Basically not a big deal

Code: Select all

@echo off &setlocal
set "root=C:\somewhwere"
set "folder=resfiles"

set /a "count=0,size=0"
for /f "delims=" %%i in ('dir /ad /b /s "%root%\*%folder%"^|findstr /eic:"\\%folder%"') do (
  set /a "count+=1"
  for /f "tokens=1,2 delims=: " %%a in ('robocopy "%%i" "%%i" /L /S /NJH /BYTES /FP /NC /NDL /NFL /TS /XJ /R:0 /W:0') do if /i "%%a"=="Bytes" set /a "size+=%%b"
)

echo %count%
echo %size%
pause
But now you have the SET /A statement to summerize the the sizes of the found directories. I can't see on your desktop and thus, I have no idea if the 32 bit limit I've been talking about might be problematic in your case. However, anything above 2147483647 isn't recognized as a numeric value anymore. In case of the size this means 2GB is already one byte too much.

Steffen

terryhenderson
Posts: 16
Joined: 22 Jul 2018 04:59

Re: Get the total size of all subdirectories with a specific name

#5 Post by terryhenderson » 29 Sep 2020 13:55

I'm more than obliged to you Steffen, thank you very very much.
My best regards,

T.

Post Reply