[已解決]CMD顯示所有FOLDER SIZE 問題

本帖最後由 bongbong3481 於 2023-10-19 19:17 編輯

A FOLDER 入邊有N個FOLDER (N1,N2...),  每個
N1,N2....folder 都有sub folder ,但我想睇到每個N1,N2.....FOLDER 的SIZE。

因為window file manager 沒辦法成批folder 顯示size ,需要遂個folder right click 睇,所以想睇下有冇d command 做到。

(試過用dir/s ,但佢連所有sub folder 的sub folder 都顯示埋,吾係想要既結果。用python 解決到,但遇到大folder 就計算好耐,速度好慢)

======

樓下仁兄介紹用du ,真是正野

https://learn.microsoft.com/en-us/sysinternals/downloads/du


以下教學說明:
https://www.bytesizedalex.com/sysinternals-du/

本帖最後由 s20012797 於 2023-10-17 22:26 編輯

Here's a simple PowerShell script that should do the trick. This script will recursively calculate the size of each subfolder in a specified folder and display the results.


# Specify the root folder
$rootFolder = "C:\path\to\your\folder"

# Get all subfolders
$folders = Get-ChildItem -Path $rootFolder -Recurse -Directory

# Loop through each folder and calculate size
foreach ($folder in $folders) {
    # Get all files in the folder, recursively
    $files = Get-ChildItem -Path $folder.FullName -Recurse -File

    # Calculate folder size by summing file sizes
    $folderSize = ($files | Measure-Object -Property Length -Sum).Sum / 1MB

    # Output folder path and size
    Write-Output ("{0}: {1:N2} MB" -f $folder.FullName, $folderSize)
}


You just need to replace `"C:\path\to\your\folder"` with the path to your root folder. This script will then calculate and display the size of each subfolder in that root folder.

Please note that this script calculates folder sizes by summing the sizes of all files in each folder, including files in subfolders. The size is displayed in megabytes (MB) for readability. If you prefer a different unit (like kilobytes or gigabytes), you can adjust the division factor in the `$folderSize` calculation.

Also, please be aware that calculating folder sizes can take some time for large folders or folders with many files or subfolders. The script may appear to be doing nothing while it's calculating, but it should eventually output the sizes of all subfolders. If you're dealing with particularly large folders, you might want to consider using a more efficient method or tool designed for this purpose.

TOP

本帖最後由 bongbong3481 於 2023-10-18 09:12 編輯

回覆 2# s20012797

公司機禁左powershell, 用Bing chat轉左做cmd batch script ,不過計吾到folder size。另外用vba 計一個大folder size,都係好慢。似乎是要加總folder 內所有sub folder and file 關係,所以速度比較慢。

補充: hard disk 讀寫速度都好大影響,系SSD 計算FOLDER SIZE 非常之快


±++++++++++++++++++++++++
@echo off
setlocal enabledelayedexpansion
:: Specify the root folder

set "rootFolder=D:\backup\share_Drive"
:: Loop through each subfolder and calculate size

for /d %%D in ("%rootFolder%\*") do (

    set "folderSize=0"
    :: Loop through each file in the folder, recursively

    for /r %%F in ("%%D\*") do (

        set /a "folderSize+=%%~zF"

    )
    :: Convert folder size to MB
    set /a "folderSize=!folderSize! / 1024 / 1024"

    :: Output folder path and size

    echo %%D: !folderSize! MB
)
pause

TOP

回覆  s20012797

公司機禁左powershell, 用Bing chat轉左做cmd batch script ,不過計吾到folder size。 ...
bongbong3481 發表於 2023/10/18 08:41


吾比寫爬蟲的確冇咩好搞,用Dir做個Log係花時間但方便過盲試

TOP

回覆 4# s20012797

我覺得顯示所有folder size,  普通方法都是慢,好多方法都是要加總folder and sub folder , 如果是ssd 速度就快好多。

我用到selenium ,我估玩到爬蟲,不過暫時用python都是解決工作遇到既問題為主,另外自己特別有興趣既,都會學下。呢幾個月,用python 寫左好多種類既program 仔(抄d 改d) ,我覺得對工作都好有用,下面是暫時寫左既program,多到自己有d 吾記得。我覺得都算多元化。

e.g.
1.task schedule
2.pdf 合併/抽頁
3.圖片轉文字/pdf 轉文字/pdf 轉圖片
4.自動儲存keyboard 打既文字,放係excel
5.prevent window unlock
6.將n 個folder and  sub folder 內既file  copy or move 去1個folder(重複既加編號)
7.將excel 資料輸入去網頁 或暫將網頁資料搬去excel
9.download 網頁資料
10. 計算folder 內的sub folder size (速度係慢,但解決到問題)
11.用hot key print screen/ 定時print screen
12.gif maker
13.用pywinauto n pyautogui 操控軟件處理日常工作
14.修改csv file 某d 內容,再save 番成同名csv
15.n 個excel 合併為1個excel
16.n個excel sheet 合併成1個sheet
17.將成批csv 轉為excel

TOP

本帖最後由 epcsub 於 2023-10-19 05:01 編輯

https://learn.microsoft.com/en-us/sysinternals/downloads/du
佢有一個 Do not recurse 選項

TOP

du喺基於linux系統之下 一般都快過絕大多數其他方法
windows嘅implementation唔太清楚
你可以即管試吓

TOP

回覆 7# epcsub


    Du 呢個功能好強大,研究下先

TOP

如果du出嘅數紿終未能夠滿足你要求
你可以把du嘅output再放落python做format,計算
只係用du佢最強項:快讀取

TOP

如果du出嘅數紿終未能夠滿足你要求
你可以把du嘅output再放落python做format,計算
只係用du佢最強項:快讀 ...
epcsub 發表於 2023/10/19 11:56


行到python仲洗等你du做嘅format計算???

TOP