Batch: Retrieve the size of different folders
brucine Posted messages 24501 Registration date Status Membre Last intervention -
Hello,
I have more of a question than a problem.
So, I have this batch script that displays the size of the different folders in the folder where the script runs in bytes / Ko / Mo / Go.
So for that, I have this little script:
FOR /D %%d IN (*) DO ( FOR /R "." %%s IN (*) DO ( SET /A size+=%%~zs ) SET doss=%%~nd ) ECHO %doss% : %size% o SET /A size_ko=%size%/1024 ECHO %doss% : ~%size_ko% Ko SET /A size_mo=%size_ko%/1024 ECHO %doss% : ~%size_mo% Mo SET /A size_go=%size_mo%/1024 ECHO %doss% : ~%size_go% Go
This little script works great, but (yes because there always has to be something wrong) it's not displaying the correct size...
It currently analyzes a folder of ~78 Go (I don't care if the script is fast or not, I have all the time in the world) but only displays that I have ~838 Mo. If anyone has an idea why or how.
I should specify that the different folders only contain images in PNG format and are named like this: frame_%num.png.
Are file/folder size analyses limited to avoid potentially long tasks, or are they simply limited by a maximum number of different files per folder?
1 réponse
Hello,
To solve the problem, I would say to "delay" the size variable like this:
setlocal enabledelayedexpansion FOR /D %%d IN (*) DO ( FOR /R "." %%s IN (*) DO ( SET /A size+=%%~zs ) SET doss=%%~nd ) ECHO !doss! : !size! o SET /A size_ko=!size!/1024 ECHO !doss! : ~%size_ko% Ko SET /A size_mo=%size_ko%/1024 ECHO !doss! : ~%size_mo% Mo SET /A size_go=%size_mo%/1024 ECHO !doss! : ~%size_go% Go
-
You could also simplify the script like this:
setlocal enabledelayedexpansion FOR /R "." %%s IN (*) DO SET /A size+=%%~zs ECHO !CD! : %size% o SET /A size_ko=%size%/1024 ECHO !CD! : ~%size_ko% Ko SET /A size_mo=%size_ko%/1024 ECHO !CD! : ~%size_mo% Mo SET /A size_go=%size_mo%/1024 ECHO !CD! : ~%size_go% Go
Hello,
Don't forget that in the case of a file larger than 2 GB, we will be stopped by the calculation limit of 2^31.
Therefore, before the calculation, we must limit the display of the size in bytes to 9 digits:
SET/A "SIZE9=%SIZE:~0,9%"
If I want to nitpick to the extreme and display the number with decimals, I will also need to test the length of the string in bytes to apply the correct output format after division based on whether we are in the range of 1 to 10, 10 to 99, or 100 to 999 MB.
For those who disagree, try randomly with a 5.50 GB folder:
that I will consequently have no way to divide by 1024