获取子文件夹的大小只使用批处理命令 [英] Get size of the sub folders only using batch command
问题描述
我通过我的基本文件夹名( C:\\用户\\ IAM \\桌面\\ MHW \\ *
)的脚本,并希望得到底层子文件夹的大小。下面code不工作。需要帮助解决它。
I am passing my base folder name (C:\Users\IAM\Desktop\MHW\*
) in script and want to get the size of the underlying sub folders. The below code is not working. Need help to fix it.
@echo off
setLocal EnableDelayedExpansion
FOR /D %%G in ("C:\Users\IAM\Desktop\MHW\*") DO (
set /a value=0
set /a sum=0
FOR /R "%%G" %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
@echo %%G: !sum! K
)
pause
从我的理解,值%% G不获取传递给第二个for循环。
From my understanding, the value "%%G" is not getting passed to the second FOR loop.
推荐答案
可惜你不能传递给为/ R
由另一个循环<$ C $根目录路径C>为变量,也不是一个delayedly扩展型变量,你必须使用一个正常扩张变量(%VAR%
)或参数引用(%〜1
)。
Unfortunately you cannot pass a root directory path to a for /R
loop by another for
variable nor a delayedly expanded variable, you must use a normally expanded variable (%var%
) or an argument reference (%~1
).
您可以帮助自己走出经由呼叫放置
为/ R
循环在子例程从主程序调code>。通过该变量保存的结果和根目录路径,通过作为参数,并扩大他们像%〜1
和%〜2
中的子程序,分别
You can help yourself out by placing the for /R
loop in a sub-routine that is called from the main routine via call
. Pass the variable holding the result and the root directory path over as arguments and expand them like %~1
and %~2
in the sub-routine, respectively.
@echo off
setlocal EnableDelayedExpansion
for /D %%G in ("C:\Users\IAM\Desktop\MHW\*") do (
rem Call the sub-routine here:
call :SUB sum "%%~G"
echo %%~G: !sum! KiB
)
pause
endlocal
exit /B
:SUB rtn_sum val_path
rem This is the sub-routine expecting two arguments:
rem the variable name holding the sum and the directory path;
set /A value=0, sum=0
rem Here the root directory path is accepted:
for /R "%~2" %%I in (*) do (
rem Here is some rounding implemented by `+1024/2`:
rem to round everything down, do not add anything (`+0`);
rem to round everything up, add `+1024-1=1023` instead;
set /A value=^(%%~zI+1024/2^)/1024
set /A sum+=value
)
set "%~1=%sum%"
exit /B
注意设置/ A
能够仅在32位房符号整数arithetics的,因此,如果一个文件是2吉布大或以上,或结果之
2超出 31 - 1,您会收到错误的结果。
Note that set /A
is capable of signed integer arithetics in a 32-bit room only, so if a file is 2 GiB big or more, or the result in sum
exceeds 231 - 1, you will receive wrong results.
这篇关于获取子文件夹的大小只使用批处理命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!