请参阅以下原始问题,了解不同方式的一些测试比较:
所以到目前为止我尝试了两种方法:
1.使用Get Folder Size from Windows Command Line中的代码遍历目录:
@echo off
set size=0
for /r %%x in (folder\*) do set /a size+=%%~zx
echo %size% Bytes
2.保存a的输出
'dir %folder% /s /a'
成一个文本文件,然后读入底部的大小
3.我现在尝试的最后一种方法是使用 du(来自 MS 的磁盘实用工具 - https://technet.microsoft.com/en-us/sysinternals/bb896651.aspx)。
现在除了#3之外,这两种方式对于我需要的东西(数千个文件)来说似乎都太慢了。所以问题是其中哪一个是最快的/应该是最快的,以及是否有任何其他快速(er)方法来获取包含 100k+ 个文件(并且有 100 个文件夹)的文件夹内容的大小
开始编辑:
下面是我进行比较的非常老套的方法(屠宰我的程序以查看一些输出)
有一些小错误,如选项 3 之类的部分会失败,因为它试图处理大于 32 位限制的数字,我当然还有一些问题,但我认为一般的时间安排是显而易见的,除非我真的搞砸了我的逻辑。
选项 I:遍历目录,使用 VB 脚本从“dir”读取文本输出并查找最后的大小 + 将其转换为 MB(最初是从其他地方得到的,但实际上我失去了它的位置来自) 选项 II:使用 findstr 管道进行迭代并直接输出结果(不转换为 MB) - 来自 @MC ND 选项 III:使用紧凑命令进行迭代 - 来自 @npocmaka 选项 IV:来自 @user1016274 - 使用 robocoby
(还有更多答案,但这些是我能够合并的答案)
这些是我得到的结果,它们之间的相关性非常一致,robocopy 把它们吹走了
选项 I 和选项 II 通常接近,选项 II 稍好一些(两者都在 1 分 10 秒到 2 分 10 秒之间,不确定差异来自哪里) 第 III 部分 - 16-17 分钟 第 IV 部分 - 10-20 秒
@echo OFF
setlocal enabledelayedexpansion
REM OPTION I - directory iteration
REM OPTION II - iteration with findstr pipe
REM OPTION III - compact
:MAIN
REM Initialize log filename
for /f "delims=" %%a in ('echo %date:~10,4%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%%time:~6,2%') do @set LOGFILEPOSTFIX=%%a
set LOGFILEPOSTFIX=%date:~10,4%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%%time:~6,2%
set TIMESTAMP=%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %TIMESTAMP%
set "LOGFILE=Proj_not_in_db_%LOGFILEPOSTFIX%.log"
set option=1
set TIMESTAMP=%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %TIMESTAMP% - PART I ---- Directory Listing into file, iterate through the sizes of all files inside folder >> %LOGFILE%
echo %TIMESTAMP% - PART I
call :PROCESSFOLDER
set TIMESTAMP=%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %TIMESTAMP% - PART I ---- END >> %LOGFILE%
echo %TIMESTAMP% - PART I - END
set option=2
set TIMESTAMP=%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %TIMESTAMP% - PART II findstr pipe ---- >> %LOGFILE%
echo %TIMESTAMP% - PART II
call :PROCESSFOLDER
set TIMESTAMP=%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %TIMESTAMP% - PART II ---- END>> %LOGFILE%
echo %TIMESTAMP% - PART II - END
set option=3
set TIMESTAMP=%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %TIMESTAMP% - PART III compact ---- >> %LOGFILE%
echo %TIMESTAMP% - PART III
call :PROCESSFOLDER
set TIMESTAMP=%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %TIMESTAMP% - PART III ---- END>> %LOGFILE%
echo %TIMESTAMP% - PART III - END
set option=4
set TIMESTAMP=%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %TIMESTAMP% - PART IV robocopy ---- >> %LOGFILE%
echo %TIMESTAMP% - PART IV
call :PROCESSFOLDER
call :CLEANUP
echo FINAL
pause
goto :EOF
:PROCESSFOLDER
echo C:\Windows
echo Processing C:\Windows >> %LOGFILE%
break > projects_in_folder.tmp
for /f "tokens=1-4,* SKIP=7" %%b IN ('dir "C:\Windows" /Q /TW /AD') do (
set _folder=%%f
REM Don't write the 2 lines at the end displaying summary information
if NOT "%%e" EQU "bytes" (
SET _folder=!_folder:~23!
echo !_folder!,%%b>> projects_in_folder.tmp
)
)
set "folder_path=C:\Windows"
call :COMPARE
goto :EOF
:COMPARE
set file_name=%folder_path:\=_%
break > "%file_name%.txt"
if %option%==4 (
set "full_path=C:\Windows"
call :GETFOLDERINFO4
set TIMESTAMP=%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %TIMESTAMP% - PART IV ---- END>> %LOGFILE%
echo %TIMESTAMP% - PART IV - END
)
for /f "tokens=1,2* delims=," %%a in (projects_in_folder.tmp) do (
for /f "tokens=1,* delims=_" %%x in ("%%a") do (
set "projcode=%%x"
)
set full_path=%folder_path%\%%a
if %option%==1 call :GETFOLDERINFO
if %option%==2 call :GETFOLDERINFO2
if %option%==3 call :GETFOLDERINFO3
echo PROJ: %%a SIZE: !totalsize! LASTMODIFIED: %%b >> %LOGFILE%
)
goto :EOF
:GETFOLDERINFO2
set "size=0"
set target=!full_path!
for /f "tokens=3,5" %%a in ('
dir /a /s /w /-c "%target%"
^| findstr /b /l /c:" "
') do if "%%b"=="" set "size=%%a"
echo %size%
set totalsize=%size%
goto :EOF
:GETFOLDERINFO4
pushd "%full_path%" || goto :EOF
setlocal
for /f "tokens=1-10,* delims= " %%a in ('
robocopy %full_path% %TEMP% /S /L /BYTES /XJ /NFL /NDL /NJH ^| find "Bytes"
') do echo %full_path%: %%c
popd
goto :EOF
:GETFOLDERINFO
set totalsize=0
dir "%full_path%" /s /a > size.txt
REM Run VBScript that outputs size in MB which is saved
pushd %~dp0
start /b "" cscript /nologo foldersize.vbs
FOR /F "usebackq tokens=*" %%r in (`CSCRIPT "foldersize.vbs"`) DO SET totalsize=%%r
echo bla > nul
goto :EOF
:GETFOLDERINFO3
set "last=#"
set "_size="
for /f "tokens=1 delims= " %%s in ('compact /s:"%full_path%" /q ') do (
set "_size=!last!"
set "last=%%s"
)
set "_size=%_size: =%"
set "_size=%_size: =%"
set "_size=%_size:.=%"
set "_size=%_size:,=%"
set "_size=%_size: =%"
echo folder size is : %_size% bytes
set totalsize=%_size%
goto :EOF
:CLEANUP
DEL /Q /S projects_in_folder.tmp
DEL /Q /S size.txt
goto :EOF