如何使用批处理以字节为单位获取文件夹的大小

时间:2013-07-25 12:06:04

标签: windows for-loop batch-file cmd directory

如果我检查我文件夹的大小,我会得到:

for %i in (*) do @set /a size+=%~zi
166576251665763016658914166589201665917416659428166596821665993616660190166601951666038416660487168652871687269016872716169860941698686116986931169872071698728616987313169875941698775316987908169884331700481717005585170056001700563417005891170059181700605117007696170076961700782617021650170218471702192817022003170220911702211617022141170222091702227017022755170228501702370517023960170242831702453617024848170252511702526817578552175800371758003717580037175800371758003717580037175826531758490317591537175919931759206017592095175921301764998617650665176520351765318217654163176541681772380017723825177251031772519925614095256149392561514625615899256181532599996926105233264957532672798526897833268979242689853526898590268986582689871926898790269001782690062626900643269008432690103926907695269077312690853826909921269100822691020226910240269105802691578626915899269343312693438226935123269842752698438326984697269990372701337927013912270139282701420927014362270144002701448427014572274675622746760127468336275041762753080027530915275319062753190627535850275367832756033527560455275605872756248927562632275846482783706427968251279696822797111327972544279739752797540627976865279782962797972727981158279811582805130228051656
echo %size%
28051656

但是dir命令说大小是1,907,481,021个字节!出了什么问题?我怎样才能得到合适的尺码?我有这个Batch file that returns folder size,但我不希望大单位为MB,GB。我想把大小放在变量中。

感谢。

5 个答案:

答案 0 :(得分:4)

这将返回文件夹及其子目录的总和。

@echo off
call :size "c:\folder1" 
call :size "c:\folder2" 
pause
goto :eof
:size
for /f "tokens=3" %%b in ('dir /s "%~1" 2^>nul ^|find " File(s) "') do  set "n=%%b"
for /f "tokens=1-4 delims=," %%c in ("%n%") do (
echo %%c%%d%%e%%f bytes [%n%] in "%~1"
)

答案 1 :(得分:2)

试试这个,它用dir命令读取文件夹大小:

@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
FOR /f "tokens=3" %%a IN ('dir /a-d /-c') DO (
    SET size=!free!
    SET free=%%a
)
ECHO %size% bytes in %cd%

这适用于欧洲时间格式。对于AM/PM格式设置tokens=4
这是Aacini的解决方案。

答案 2 :(得分:1)

您正面临算术溢出。检查

 SET /A s=2147483647+1

环境变量算术是通过32位有符号整数完成的。

因此,您可以安全求和的最大数量是2^31-1 = 2147483647

答案 3 :(得分:1)

如果您确实需要以字节计数,那么您需要将数字拆分为多个变量。

与lowCount一样,低8位数字和highCount高8位数字,所以你得到16位数,分辨率高达9999TBytes。

你只需要添加一些数学函数来处理大数字。

答案 4 :(得分:1)

代码,用于存储在文本文件中的一组文件夹:

@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "file=file"
SET /a allsize=0
for /f %%x in (
'for /f "usebackqdelims=" %%a in (%file%^) do @(
    for /f "tokens=3" %%b in ('dir /a-d /-c "%%~a" ^^^|findstr /BR "[0-9]"'^) do @ECHO(%%b^)'
) do (
    FOR /f %%i IN ('awk "BEGIN {a=%%x+!allsize!; print a}"') DO SET "allsize=%%i"
)
ECHO Size of all folders listet in %file%:
awk "BEGIN {a=%allsize%; printf(\"%%10d %%s\",a,\"bytes\n\")}"
awk "BEGIN {a=%allsize%/1024; printf(\"%%10d %%s\",a,\"KB\n\")}"
awk "BEGIN {a=%allsize%/1024/1024; printf(\"%%10d %%s\",a,\"MB\n\")}"
awk "BEGIN {a=%allsize%/1024/1024/1024; printf(\"%%10.2g %%s\",a,\"GB\n\")}"

示例:

Size of all folders listet in file:
1907481021 bytes
   1862774 KB
      1819 MB
       1.8 GB