在' MB'中获取目录的大小使用批处理文件

时间:2016-03-30 06:23:32

标签: batch-file

我想使用批处理文件在C:\Temp中获取目录的大小MB。我不需要子目录或文件的大小,但需要目录本身的大小。

我在How to list all folder with size via batch file

找到答案

但它给出了字节的大小和子文件夹的大小。所以我的问题是:

如何在 MB 中获取目录本身的大小?

4 个答案:

答案 0 :(得分:3)

您可以使用混合脚本 [Batch / Vbscript] 执行此操作:

@echo off
set Folder="C:\temp"
echo The size of %Folder% is 
Call :GetSize %Folder%
pause
:GetSize
(
echo wscript.echo GetSize("%~1"^)
echo Function GetSize(MyFolder^)
echo    Set fso = CreateObject("Scripting.FileSystemObject"^)
echo    Set objFolder= fso.GetFolder(MyFolder^)  
echo    GetSize = FormatSize(objFolder.Size^)
echo End Function
echo '*******************************************************************
echo 'Function to format a number into typical size scales
echo Function FormatSize(iSize^)
echo    aLabel = Array("bytes", "KB", "MB", "GB", "TB"^)
echo    For i = 0 to 4
echo        If iSize ^> 1024 Then
echo            iSize = iSize / 1024
echo        Else
echo            Exit For
echo        End If
echo    Next
echo    FormatSize = Round(iSize,2^) ^& " " ^& aLabel(i^)
echo End Function
echo '*******************************************************************
)>%tmp%\Size.vbs
Cscript /NoLogo %tmp%\Size.vbs
Del %tmp%\Size.vbs
Exit /b

编辑:2016年3月30日@ 12:11

这是一个很好的技巧

  

Liviu的hack用于在没有临时文件的情况下批量嵌入vbscode

我刚从 npocmaka 发现感谢他

@echo off
Set Folder="c:\temp"
@cScript.EXE //noLogo "%~f0?.WSF" %Folder%  //job:info %~nx0%*
pause
@exit /b 0
<job id="info">
<script language="VBScript">
wscript.echo GetSize(WScript.Arguments(0))
Function GetSize(MyFolder)
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objFolder= fso.GetFolder(MyFolder)  
    GetSize = FormatSize(objFolder.Size)
End Function
'*******************************************************************
'Function to format a number into typical size scales
Function FormatSize(iSize)
   aLabel = Array("bytes", "KB", "MB", "GB", "TB")
   For i = 0 to 4
        If iSize > 1024 Then
            iSize = iSize / 1024
        Else
            Exit For
        End If
   Next
   FormatSize = Round(iSize,2) & " " & aLabel(i)
End Function
'*******************************************************************
</script>
</job>

答案 1 :(得分:2)

@echo off
::robocopy "%~1" %TEMP% /S /L /BYTES /XJ /NFL /NDL /NJH
for /f "tokens=2 delims=: " %%a in ('
    robocopy "%~1" %TEMP% /S /L /BYTES /XJ /NFL /NDL /NJH ^| find "Bytes"
') do set "bytes=%%a"


::1048576

set "beginJS=mshta "javascript:close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write("
set "endJS=));""




for /f %%N in (
  '%beginJS% %bytes%/1048576 %endJS%'
) do set mb=%%N
echo mb=%mb%

它需要一个参数 - 您想要计算大小的文件夹

答案 2 :(得分:1)

这是一个纯粹的批处理文件解决方案(请参阅代码中的备注以获得一些简短的解释):

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Define constants here:
set /A DIVISOR=1024 & rem (1024 Bytes = 1 KBytes, 1024 KBytes = 1 MByte,...)
set "ROUNDUP=#" & rem (set to non-empty value to round up results)

rem Get size of directory given as command line argument:
for /F "tokens=2 delims=: " %%B in ('
    robocopy "%~1" "%~1" "*.*" /L /S /XJ /BYTES /NP /NFL /NDL /NJH ^| ^
        find /I "Bytes"
') do set "BYTES=%%B"
if not defined BYTES set "BYTES=0"
rem Display result in Bytes and divide it to get KBytes, MBytes, etc.:
call :DIVIDE %BYTES% 1 RESULT
echo( Bytes:    %RESULT%
call :DIVIDE %RESULT% %DIVISOR% RESULT REST
if defined ROUNDUP if 0%REST% GTR 0 set /A RESULT+=1
echo(KBytes:    %RESULT%
call :DIVIDE %RESULT% %DIVISOR% RESULT REST
if defined ROUNDUP if 0%REST% GTR 0 set /A RESULT+=1
echo(MBytes:    %RESULT%
call :DIVIDE %RESULT% %DIVISOR% RESULT REST
if defined ROUNDUP if 0%REST% GTR 0 set /A RESULT+=1
echo(GBytes:    %RESULT%

endlocal
exit /B


:DIVIDE val_dividend val_divisor [ref_result] [ref_remainder]
rem Divide a huge number exceeding the 32-bit limitation
rem by a 32-bit number in the range from 1 to 1000000000;
rem the result might also exceed the 32-bit limitation.
setlocal EnableDelayedExpansion
set "DIVIDEND=%~1"
set "DIVISOR=%~2"
set "QUOTIENT=%~3"
set "REMAINDER=%~4"

rem Check whether dividend and divisor are given:
if not defined DIVIDEND (
    >&2 echo(Too few arguments, dividend missing^^!
    exit /B 2
) else if not defined DIVISOR (
    >&2 echo(Too few arguments, divisor missing^^!
    exit /B 2
)
rem Check whether dividend is purely numeric:
for /F "tokens=* delims=0123456789" %%N in ("!DIVIDEND!") do (
    if not "%%N"=="" (
        >&2 echo(Dividend must be numeric^^!
        exit /B 2
    )
)
rem Convert divisor to numeric value without leading zeros:
for /F "tokens=* delims=0" %%N in ("%DIVISOR%") do set "DIVISOR=%%N"
set /A DIVISOR+=0
rem Check divisor against its range:
if %DIVISOR% LEQ 0 (
    >&2 echo(Divisor value must be positive^^!
    exit /B 1
) else if %DIVISOR% GTR 1000000000 (
    >&2 echo(Divisor value exceeds its limit^^!
    exit /B 1
)
set "COLL=" & set "NEXT=" & set /A INDEX=0
rem Do a division digit by digit as one would do it on paper:
:LOOP
if "!DIVIDEND:~%INDEX%,1!"=="" goto :CONT
set "NEXT=!NEXT!!DIVIDEND:~%INDEX%,1!"
rem Remove trailing zeros as such denote octal numbers:
for /F "tokens=* delims=0" %%N in ("!NEXT!") do set "NEXT=%%N"
set /A NEXT+=0
set /A PART=NEXT/DIVISOR
set "COLL=!COLL!!PART!"
set /A NEXT-=PART*DIVISOR
set /A INDEX+=1
goto :LOOP
:CONT
rem Remove trailing zeros as such denote octal numbers:
for /F "tokens=* delims=0" %%N in ("%COLL%") do set "COLL=%%N"
if not defined COLL set "COLL=0"
rem Set return variables or display result if none are given:
if defined QUOTIENT (
    if defined REMAINDER (
        endlocal
        set "%REMAINDER%=%NEXT%"
    ) else (
        endlocal
    )
    set "%QUOTIENT%=%COLL%"
) else (
    endlocal
    echo(%COLL%
)
exit /B

基本上,作为命令行参数给出的目录内容的大小由robocopy收集,类似于npocmaka's approach;以字节为单位的结果大小存储在变量BYTES中。

之后,完成了几个子程序调用,每个调用都将结果除以1024,以获得以KB为单位的大小,然后是MBytes和GBytes。

如果存在余数以便对大小进行舍入,则除法产生的值将增加1(类似于Windows资源管理器,例如,对于非常小的文件显示1 KB)。您可以通过将变量ROUNDUP设置为空字符串来禁用此功能,这会导致四舍五入。

分部方法

由于set /A命令(及其除法运算符/)只能处理(带符号)32位整数,因此子程序:DIVIDE执行实际除法,就像执行它一样。纸。被除数(即要分割的数字)被视为一个字符串,因此可以超过32位范围;除数(即除数)但不得超过它;相反,它必须在11000000000的范围内。由:LOOP和条件goto :LOOP组成的goto :CONT循环构成一个类似于循环的结构,它遍历被除数的所有十进制数字,并在达到最后一个之后终止

子程序至少需要两个参数 - 被除数和除数的值。它甚至还接受两个 - 用于保存商的变量的名称(即除法结果)和另一个用于保存余数的变量。如果没有给出可选参数,则控制台上会显示商。

答案 3 :(得分:0)

如果您需要确切的值,请选择其他选项。但是如果你只需要一个粗略的值(cut,而不是舍入,而不是1024除数,只有1000):

@echo off
for /f "tokens=2 delims=," %%a in ('dir *^|findstr /e "Bytes"') do (
  for /f "delims= " %%i in ("%%a") do set size=%%i
)
echo size in Bytes: %size%
echo that's about %size:~0,-4% KB
echo or about %size:~0,-8% MB
echo or about %size:~0,-12% GB

优点:这只是字符串操作,不包含数学,所以它甚至可以在32位整数之外工作(大小限制在2GB左右)。