For Loop to Retrieve Average File Sizes
Posted: 28 Apr 2009 20:01
Hello all,
I'm trying to get the average file size of three file types within a folder. The script below partially works, but only on the last of the three file types, and therefore only echoes one set of results. If I move the final closing parenthesis to the end of the script (from line 17 to line 32), I get all three file types displayed, but no actual numerical results. I think I need another loop, or a function of some sort, but I'm quite stuck. Any advice is appreciated.
Also, line 19 (echo.Found %num% *%%A files) does not return the value for %%A. I know that's a clue, but I lack the know-how to continue.
The way it is now, I get these results:
And if I move the closing parenthesis to line 32, I get this:
That's the layout I want, but as you see there's no data.
Thank you, and I'm looking forward to advice from the masters...
I'm trying to get the average file size of three file types within a folder. The script below partially works, but only on the last of the three file types, and therefore only echoes one set of results. If I move the final closing parenthesis to the end of the script (from line 17 to line 32), I get all three file types displayed, but no actual numerical results. I think I need another loop, or a function of some sort, but I'm quite stuck. Any advice is appreciated.
Code: Select all
@echo off
SETLOCAL
title Average File Sizes
set LOC=c:\logs
set MATH="C:\Program Files\math\math.exe"
set DIV=---------------------------------
for %%A in (.log,.txt,.stats) do (
echo.%DIV%
for /f "usebackq tokens=1,3 delims= " %%X in (`dir %LOC%\*%%A ^| findstr "File(s)"`) do (
set num=%%X
set size=%%Y
)
)
set "size=%size:,=%"
echo.Found %num% *%%A files
echo.
for /f "usebackq" %%M in (`%MATH% %size%/%num%`) do set avgB=%%M
for /f "usebackq" %%N in (`%MATH% %size%/%num%/1024`) do set avgKB=%%N
for /f "usebackq" %%O in (`%MATH% %size%/%num%/1024/1024`) do set avgMB=%%O
for /f "usebackq" %%P in (`%MATH% %size%/%num%/1024/1024/1024`) do set avgGB=%%P
echo.Average Size is:
echo.
echo. B %avgB%
echo.KB %avgKB%
echo.MB %avgMB%
echo.GB %avgGB%
echo.
pause
Also, line 19 (echo.Found %num% *%%A files) does not return the value for %%A. I know that's a clue, but I lack the know-how to continue.
The way it is now, I get these results:
Code: Select all
---------------------------------
---------------------------------
---------------------------------
Found 2 *%A files
Average Size is:
B 41216107
KB 40250.1045
MB 39.3067427
GB 0.0383854909
Press any key to continue . . .
And if I move the closing parenthesis to line 32, I get this:
Code: Select all
---------------------------------
Found *.log files
Average Size is:
B
KB
MB
GB
---------------------------------
Found *.txt files
Average Size is:
B
KB
MB
GB
---------------------------------
Found *.stats files
Average Size is:
B
KB
MB
GB
Press any key to continue . . .
That's the layout I want, but as you see there's no data.
Thank you, and I'm looking forward to advice from the masters...