我在批处理文件中有以下代码:
set /p user="Enter username: " %=%
cd w:\Files
@echo off
setLocal EnableDelayedExpansion
set /a value=0
set /a sum=0
set /a valA=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
set /a sum=!sum!/1024
)
@echo Size of "FILES" is: !sum! MB
@echo off
FOR /R %1 %%I IN (*) DO (
set /a sum=!sum!/1024
set /a valA=!sum!
)
@echo Size of "FILES" is: !sum! GB
cd W:\Documents and Settings\%user%\Desktop
@echo off
setLocal EnableDelayedExpansion
set /a value=0
set /a sum=0
set /a valB=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
set /a sum=!sum!/1024
)
@echo Size of Desktop is: !sum! MB
@echo off
FOR /R %1 %%I IN (*) DO (
set /a sum=!sum!/1024
set /a valB=!sum!
)
@echo Size of Desktop is: !sum! GB
它还会检查一些其他文件夹,但您应该明白这一点。
我得到了这个输出:
C:\Users\pprescott\Desktop>cd w:\Files
Size of "FILES" is: 215 MB
Size of "FILES" is: 0 GB
Size of Desktop is: 215 MB
Size of Desktop is: 0 GB
Size of Favorites is: 215 MB
Size of Favorites is: 0 GB
Size of Documents is: 215 MB
Size of Documents is: 0 GB
Total size is: 0 MB
Total size is: 0 GB
Press any key to continue . . .
这是为了计算旧xp机器上的文件夹大小以准备数据传输。 xp机器映射到驱动器W。
答案 0 :(得分:2)
尝试删除for /r
参数%1
:
FOR /R %%I IN (*) DO (
试试这段代码:
@ECHO OFF &SETLOCAL
FOR /R "w:\Files" %%I IN (*) DO set /a asum+=%%~zI
SET /a sum=asum/1048576
echo Size of "FILES" is: %sum% MB
set /a sum=asum/1073741824
echo Size of "FILES" is: %sum% GB
FOR /R "W:\Documents and Settings\%user%\Desktop" %%I IN (*) DO set /a asum+=%%~zI
SET /a sum=asum/1048576
echo Size of "DESKTOP" is: %sum% MB
set /a sum=asum/1073741824
echo Size of "DESKTOP" is: %sum% GB
答案 1 :(得分:1)
使用dir
命令输出。见这个例子
这将获取文件夹大小并将其转换为不同的值。 请注意,转换仅适用于最大2千兆字节的数值。
@echo off
for /f "tokens=3" %%A IN ('dir /s /-c ^| find /i "bytes" ^| find /v /i "free"') do set T=%%A
echo Bytes = %T%
set /a T/=1024
echo Kilobytes = %T%
set /a T/=1024
echo Megabytes = %T%
set /a T/=1024
echo Gigabytes = %T%
输出:
C:\Users\User\Desktop>Test.bat
Bytes = 18280552
Kilobytes = 17852
Megabytes = 17
Gigabytes = 0
答案 2 :(得分:1)
这应该以字节为单位报告树的大小,最大为999 GB。
较低的答案使用VBS脚本将数字转换为GB(删除小数)
@echo off
call :size "w:\Files"
call :size "W:\Documents and Settings\%user%\Desktop"
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 "%~1"
)
)
返回大约GB数字的解决方案
@echo off
call :size "w:\Files"
call :size "W:\Documents and Settings\%user%\Desktop"
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 (
>"%temp%\VBS.vbs" echo Set fso = CreateObject^("Scripting.FileSystemObject"^) : Wscript.echo ^(%%c%%d%%e%%f/1024/1024^)
for /f "tokens=1 delims=." %%z in ('cscript /nologo "%temp%\VBS.vbs"') do echo %%z GB "%~1"
del "%temp%\VBS.vbs"
)
)