Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- ::
- :: Calculates the average size of files (KB) for a given directory
- :: DOS/Windows batch files only support integers for numbers and only 32-bit integers,
- :: so to prevent rollover of the running sum of the file sizes we maintain the size in KB,
- :: and keep a seperate rolling sum of the KB modula of file sizes, which we'll use to round
- :: the sum up by 1KB if the average of the modulas is >= 1/2 a KB
- ::
- :: set initial running count/sum vars
- set /a fileCount = 0
- set /a fileSizeSumKb_Main = 0
- set /a fileSizeSumKb_Modula = 0
- :: Iterate through each file in the path specified on the command line
- FOR %%A in (%1\*.*) do CALL :AddFileToRunningTotals %%A
- :: if the sum of the 1KB-modula of every file's size is > = 512 then we round up by 1KB
- set /a fileSizeSumKb_With_Modula = %fileSizeSumKb_Main% + (%fileSizeSumKb_Modula%/1024) + (%fileSizeSumKb_Modula% %% 1024 / 512)
- :: Calculate the average. If the modula of the division to derive the average >= 0.50 then round up the result by 1KB
- :: Note: we have to calculate > = 0.50 by multiplying the modula result by 100 then dividing 50 since we can only use integer math
- set /a fileSizeAverageKb = (%fileSizeSumKb_With_Modula% / %fileCount%) + (%fileSizeSumKb_With_Modula% %% %fileCount% * 100 / %fileCount% / 50)
- :: Display results and exit
- echo fileCount: %fileCount%, total size: %fileSizeSumKb_With_Modula% KB, Average File Size: %fileSizeAverageKb% KB
- GOTO:EOF
- :: Adds the current file to the running total of files and file sizes
- :AddFileToRunningTotals
- set FILELEN=%~z1
- IF [%FILELEN%] == [] GOTO:EOF
- set /a fileSizeSumKb_Main += (%FILELEN% / 1024)
- set /a fileSizeSumKb_Modula += ((%FILELEN% %% 1024 + 1024) %% 1024)
- set /a fileCount += 1
- GOTO:EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement