![](/img/trans.png)
[英]Windows Command Line - Determine Folder Contents Size and save as numerical value
[英]Get Folder Size from Windows Command Line
Windows 是否可以在不使用任何第三方工具的情況下從命令行獲取文件夾的大小?
我想要的結果與您在 windows 資源管理器 → 屬性中右鍵單擊該文件夾時得到的結果相同。
有一個內置的 Windows 工具:
dir /s 'FolderName'
這將打印很多不必要的信息,但最終將是這樣的文件夾大小:
Total Files Listed:
12468 File(s) 182,236,556 bytes
如果您需要包含隱藏文件夾,請添加/a
。
您可以遞歸地添加大小(以下是批處理文件):
@echo off
set size=0
for /r %%x in (folder\*) do set /a size+=%%~zx
echo %size% Bytes
但是,這有幾個問題,因為cmd
僅限於 32 位有符號整數算法。 所以它的大小會超過 2 GiB 錯誤1 。 此外,它可能會多次計算符號鏈接和連接點,因此它充其量只是一個上限,而不是真正的大小(盡管使用任何工具都會遇到這個問題)。
另一種選擇是 PowerShell:
Get-ChildItem -Recurse | Measure-Object -Sum Length
或更短:
ls -r | measure -sum Length
如果你想讓它更漂亮:
switch((ls -r|measure -sum Length).Sum) {
{$_ -gt 1GB} {
'{0:0.0} GiB' -f ($_/1GB)
break
}
{$_ -gt 1MB} {
'{0:0.0} MiB' -f ($_/1MB)
break
}
{$_ -gt 1KB} {
'{0:0.0} KiB' -f ($_/1KB)
break
}
default { "$_ bytes" }
}
您可以直接從cmd
使用它:
powershell -noprofile -command "ls -r|measure -sum Length"
1我在批處理文件中確實有一個部分完成的 bignum 庫,它至少可以正確地獲得任意精度的整數加法。 我真的應該發布它,我想:-)
我建議從 Microsoft 提供的 Sysinternals Suite 下載實用程序 DU,網址為http://technet.microsoft.com/en-us/sysinternals/bb896651
usage: du [-c] [-l <levels> | -n | -v] [-u] [-q] <directory>
-c Print output as CSV.
-l Specify subdirectory depth of information (default is all levels).
-n Do not recurse.
-q Quiet (no banner).
-u Count each instance of a hardlinked file.
-v Show size (in KB) of intermediate directories.
C:\SysInternals>du -n d:\temp
Du v1.4 - report directory disk usage
Copyright (C) 2005-2011 Mark Russinovich
Sysinternals - www.sysinternals.com
Files: 26
Directories: 14
Size: 28.873.005 bytes
Size on disk: 29.024.256 bytes
當您在使用它時,請查看其他實用程序。 它們是每個 Windows Professional 的救星
單線:
powershell -command "$fso = new-object -com Scripting.FileSystemObject; gci -Directory | select @{l='Size'; e={$fso.GetFolder($_.FullName).Size}},FullName | sort Size -Descending | ft @{l='Size [MB]'; e={'{0:N2} ' -f ($_.Size / 1MB)}},FullName"
相同但僅限 Powershell:
$fso = new-object -com Scripting.FileSystemObject
gci -Directory `
| select @{l='Size'; e={$fso.GetFolder($_.FullName).Size}},FullName `
| sort Size -Descending `
| ft @{l='Size [MB]'; e={'{0:N2} ' -f ($_.Size / 1MB)}},FullName
這應該產生以下結果:
Size [MB] FullName
--------- --------
580,08 C:\my\Tools\mongo
434,65 C:\my\Tools\Cmder
421,64 C:\my\Tools\mingw64
247,10 C:\my\Tools\dotnet-rc4
218,12 C:\my\Tools\ResharperCLT
200,44 C:\my\Tools\git
156,07 C:\my\Tools\dotnet
140,67 C:\my\Tools\vscode
97,33 C:\my\Tools\apache-jmeter-3.1
54,39 C:\my\Tools\mongoadmin
47,89 C:\my\Tools\Python27
35,22 C:\my\Tools\robomongo
如果您的計算機中安裝了 git(越來越普遍),只需打開 MINGW32 並輸入: du folder
我建議使用我編寫的https://github.com/aleksaan/diskusage實用程序。 非常簡單和樂於助人。 而且非常快。
只需輸入命令外殼
diskusage.exe -path 'd:/go; d:/Books'
並獲取按大小排列的文件夾列表
1.| DIR: d:/go | SIZE: 325.72 Mb | DEPTH: 1 2.| DIR: d:/Books | SIZE: 14.01 Mb | DEPTH: 1
此示例在 HDD 上以272 毫秒執行。
您可以增加要分析的子文件夾的深度,例如:
diskusage.exe -path 'd:/go; d:/Books' -depth 2
不僅可以獲取選定文件夾的大小,還可以獲取其子文件夾的大小
1.| DIR: d:/go | SIZE: 325.72 Mb | DEPTH: 1 2.| DIR: d:/go/pkg | SIZE: 212.88 Mb | DEPTH: 2 3.| DIR: d:/go/src | SIZE: 62.57 Mb | DEPTH: 2 4.| DIR: d:/go/bin | SIZE: 30.44 Mb | DEPTH: 2 5.| DIR: d:/Books/Chess | SIZE: 14.01 Mb | DEPTH: 2 6.| DIR: d:/Books | SIZE: 14.01 Mb | DEPTH: 1 7.| DIR: d:/go/api | SIZE: 6.41 Mb | DEPTH: 2 8.| DIR: d:/go/test | SIZE: 5.11 Mb | DEPTH: 2 9.| DIR: d:/go/doc | SIZE: 4.00 Mb | DEPTH: 2 10.| DIR: d:/go/misc | SIZE: 3.82 Mb | DEPTH: 2 11.| DIR: d:/go/lib | SIZE: 358.25 Kb | DEPTH: 2
*** 服務器上的 3.5Tb 已掃描 3 分 12 秒**
這是我編寫的 powershell 代碼,用於列出當前目錄下所有文件夾的大小和文件數。 您可以根據需要隨意重復使用或修改。
$FolderList = Get-ChildItem -Directory
foreach ($folder in $FolderList)
{
set-location $folder.FullName
$size = Get-ChildItem -Recurse | Measure-Object -Sum Length
$info = $folder.FullName + " FileCount: " + $size.Count.ToString() + " Size: " + [math]::Round(($size.Sum / 1GB),4).ToString() + " GB"
write-host $info
}
此代碼經過測試。 你可以再檢查一下。
@ECHO OFF
CLS
SETLOCAL
::Get a number of lines contain "File(s)" to a mytmp file in TEMP location.
DIR /S /-C | FIND "bytes" | FIND /V "free" | FIND /C "File(s)" >%TEMP%\mytmp
SET /P nline=<%TEMP%\mytmp
SET nline=[%nline%]
::-------------------------------------
DIR /S /-C | FIND "bytes" | FIND /V "free" | FIND /N "File(s)" | FIND "%nline%" >%TEMP%\mytmp1
SET /P mainline=<%TEMP%\mytmp1
CALL SET size=%mainline:~29,15%
ECHO %size%
ENDLOCAL
PAUSE
嘗試:
SET FOLDERSIZE=0
FOR /F "tokens=3" %A IN ('DIR "C:\Program Files" /a /-c /s ^| FINDSTR /C:" bytes" ^| FINDSTR /V /C:" bytes free"') DO SET FOLDERSIZE=%A
將 C:\\Program Files 更改為您想要的任何文件夾,如果在批處理文件中使用,請將 %A 更改為 %%A
它返回整個文件夾的大小,包括子文件夾和隱藏文件和系統文件,並處理超過 2GB 的文件夾
它確實寫入屏幕,因此如果您不想要,則必須使用臨時文件。
我想這只有在目錄相當靜態並且其內容在兩個 dir 命令的執行之間沒有變化時才有效。 也許是一種將其組合成一個命令以避免這種情況的方法,但這對我的目的有效(我不想要完整的列表;只是摘要)。
GetDirSummary.bat 腳本:
@echo off
rem get total number of lines from dir output
FOR /F "delims=" %%i IN ('dir /S %1 ^| find "asdfasdfasdf" /C /V') DO set lineCount=%%i
rem dir summary is always last 3 lines; calculate starting line of summary info
set /a summaryStart="lineCount-3"
rem now output just the last 3 lines
dir /S %1 | more +%summaryStart%
用法:
GetDirSummary.bat c:\\temp
輸出:
Total Files Listed:
22 File(s) 63,600 bytes
8 Dir(s) 104,350,330,880 bytes free
我認為您唯一的選擇將是 diruse(高度支持的 3rd 方解決方案):
不幸的是,Windows CLI 限制性很強,您也可以安裝Cygwin ,與 cmd 相比,這是一個夢想。 這將使您可以訪問移植的 Unix 工具 du,它是 Windows 上 diruse 的基礎。
抱歉,我無法使用您可以在本機 cli 上運行的命令直接回答您的問題。
我的 git 發行版得到了du.exe
。 另一個地方可能是前面提到的 Microsoft 或Unxutils 。
一旦你的路徑中有 du.exe。 這是你的fileSizes.bat
:-)
@echo ___________
@echo DIRECTORIES
@for /D %%i in (*) do @CALL du.exe -hs "%%i"
@echo _____
@echo FILES
@for %%i in (*) do @CALL du.exe -hs "%%i"
@echo _____
@echo TOTAL
@du.exe -sh "%CD%"
➪
___________
DIRECTORIES
37M Alps-images
12M testfolder
_____
FILES
765K Dobbiaco.jpg
1.0K testfile.txt
_____
TOTAL
58M D:\pictures\sample
::獲取 Dir 命令返回的行數(/-c 以消除數字分隔符:. ,)["Tokens = 3" 僅查看 Dir 中每行的第三列]
FOR /F "tokens=3" %%a IN ('dir /-c "%folderpath%"') DO set /ai=!i!+1
倒數第二行,其中是文件總和的字節數:
set /a line=%i%-1
最后獲得倒數第二行的字節數 - 第三列:
set i=0
FOR /F "tokens=3" %%a IN ('dir /-c "%folderpath%"') DO (
set /a i=!i!+1
set bytes=%%a
If !i!==%line% goto :size
)
:size
echo %bytes%
由於它不使用單詞搜索,因此不會有語言問題。
限制:
以下腳本可用於獲取和累積給定文件夾下每個文件的大小。
文件夾路徑%folder%
可以作為參數提供給此腳本 ( %1
)。
最終,結果保存在參數%filesize%
@echo off
SET count=1
SET foldersize=0
FOR /f "tokens=*" %%F IN ('dir /s/b %folder%') DO (call :calcAccSize "%%F")
echo %filesize%
GOTO :eof
:calcAccSize
REM echo %count%:%1
REM set /a count+=1
set /a foldersize+=%~z1
GOTO :eof
注意: calcAccSize
方法也可以打印文件夾的內容(上例中有注釋)
因此,這里以您最初要求的方式為您的兩個請求提供了解決方案。 它將提供人類可讀的文件大小,而沒有每個人都遇到的文件大小限制。 與 Win Vista 或更新版本兼容。 XP 僅在安裝了 Robocopy 時可用。 只需在此批處理文件上放置一個文件夾或使用下面提到的更好的方法。
@echo off
setlocal enabledelayedexpansion
set "vSearch=Files :"
For %%i in (%*) do (
set "vSearch=Files :"
For /l %%M in (1,1,2) do (
for /f "usebackq tokens=3,4 delims= " %%A in (`Robocopy "%%i" "%%i" /E /L /NP /NDL /NFL ^| find "!vSearch!"`) do (
if /i "%%M"=="1" (
set "filecount=%%A"
set "vSearch=Bytes :"
) else (
set "foldersize=%%A%%B"
)
)
)
echo Folder: %%~nxi FileCount: !filecount! Foldersize: !foldersize!
REM remove the word "REM" from line below to output to txt file
REM echo Folder: %%~nxi FileCount: !filecount! Foldersize: !foldersize!>>Folder_FileCountandSize.txt
)
pause
為了能夠方便地使用這個批處理文件,將它放在您的 SendTo 文件夾中。 這將允許您右鍵單擊一個文件夾或選擇的文件夾,單擊 SendTo 選項,然后選擇此批處理文件。
要在您的計算機上找到 SendTo 文件夾,最簡單的方法是打開 cmd,然后按原樣復制此行。
explorer C:\Users\%username%\AppData\Roaming\Microsoft\Windows\SendTo
以下單行可用於確定文件夾的大小。
帖子采用 Github Actions 格式,說明使用的 shell 類型。
shell: pwsh
run: |
Get-ChildItem -Path C:\temp -Recurse | Measure-Object -Sum Length
shell: cmd
run: |
powershell -noprofile -command "'{0:N0}' -f (ls C:\temp -r | measure -s Length).Sum"
最好使用du
,因為它簡單且一致。
安裝獨家新聞: iwr -useb get.scoop.sh | iex
iwr -useb get.scoop.sh | iex
安裝busybox: scoop install busybox
獲取目錄大小: du -d 0. -h
我今天早些時候在 22 年 4 月 27 日訪問過此頁面,在通過 SysInternals ( https://learn.microsoft.com/en-us/sysinternals/downloads/du ) 嘗試 DU 並通過管道傳輸 output 等之后。我說“我不是必須在 VBscript 中執行此操作嗎?我得到它來處理 ISOroot 文件夾和所有子文件夾的總大小。用你自己的文件夾完整路徑替換它。它非常快。
獲取文件夾大小.vbs
Dim fs, f, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder("C:\XPE\Custom\x64\IsoRoot")
s = UCase(f.Name) & " uses " & f.size & " bytes."
MsgBox s, 0, "Folder Size Info"
s= FormatNumber(f.size/1024000,2) & " MB"
MsgBox s, 0, "Folder Size Info"
s= FormatNumber(f.size/1073741824,2) & " GB"
MsgBox s, 0, "Folder Size Info"
使用 Windows Robocopy。
將其放入批處理文件中:
@echo off
pushd "%~dp0"
set dir="C:\Temp"
for /f "tokens=3" %%i in ('robocopy /l /e /bytes %dir% %dir% ^| findstr Bytes') do @echo %%i
pause
在dir
變量中設置文件夾的路徑。
for
循環使用tokens=3
從 robocopy findstr 命令獲取第三個字符串,這是以字節為單位的大小。
robocopy 中的/l
開關只列出了命令的 output,因此實際上並沒有運行。
robocopy 中的/e
開關復制子目錄,包括空目錄。
robocopy 中的/bytes
開關復制子目錄,包括空目錄。
您可以省略/bytes
開關以獲取默認文件夾屬性大小(KB、MB、GB...)
findstr | Bytes
findstr | Bytes
命令查找 output 的 robocopy 命令,其中包含總文件夾大小。
最后將索引%%i
回顯到控制台。
如果您需要將文件夾大小保存到變量替換
do @echo %%i
和
set size=%%i
如果你想通過剪貼板將文件夾大小發送到另一個程序:
echo %size% | clip
如果您需要獲取當前文件夾大小,請將批處理文件放入該文件夾並刪除set dir="C:\Temp"
並將兩個%dir%
替換為%cd%
,它代表當前目錄,如下所示:
@echo off
pushd "%~dp0"
for /f "tokens=3" %%i in ('robocopy /l /e /bytes "%cd%" "%cd%" ^| findstr Bytes') do @echo %%i
pause
我解決了類似的問題。 此頁面中的某些方法很慢,有些方法在多語言環境中存在問題(均假設為英語)。 我找到了在 cmd 中使用 vbscript 的簡單解決方法。 它在 W2012R2 和 W7 中進行了測試。
>%TEMP%\_SFSTMP$.VBS ECHO/Set objFSO = CreateObject("Scripting.FileSystemObject"):Set objFolder = objFSO.GetFolder(%1):WScript.Echo objFolder.Size
FOR /F %%? IN ('CSCRIPT //NOLOGO %TEMP%\_SFSTMP$.VBS') DO (SET "S_=%%?"&&(DEL %TEMP%\_SFSTMP$.VBS))
它設置環境變量 S_。 當然,您可以更改最后一行以直接將結果顯示為
FOR /F %%? IN ('CSCRIPT //NOLOGO %TEMP%\_SFSTMP$.VBS') DO (ECHO "Size of %1 is %%?"&&(DEL %TEMP%\_SFSTMP$.VBS))
您可以將其用作子程序或獨立的 cmd。 參數是用引號括起來的測試文件夾的名稱。
打開windows CMD並運行follow命令
dir /s c:\windows
僅獲取總大小的最簡單方法是 powershell,但仍然受到長度超過 260 個字符的路徑名不包括在總數中的限制
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.