Board logo

標題: [已解決]CMD顯示所有FOLDER SIZE 問題 [打印本頁]

作者: bongbong3481    時間: 2023-10-17 19:19     標題: [已解決]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:15

本帖最後由 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.
作者: bongbong3481    時間: 2023-10-18 08:41

本帖最後由 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
作者: s20012797    時間: 2023-10-18 20:25

回覆  s20012797

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


吾比寫爬蟲的確冇咩好搞,用Dir做個Log係花時間但方便過盲試
作者: bongbong3481    時間: 2023-10-18 22:19

回覆 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
作者: epcsub    時間: 2023-10-19 04:57

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

https://learn.microsoft.com/en-us/sysinternals/downloads/du
佢有一個 Do not recurse 選項
作者: epcsub    時間: 2023-10-19 05:16

du喺基於linux系統之下 一般都快過絕大多數其他方法
windows嘅implementation唔太清楚
你可以即管試吓
作者: bongbong3481    時間: 2023-10-19 06:52

回覆 7# epcsub


    Du 呢個功能好強大,研究下先
作者: epcsub    時間: 2023-10-19 11:56

如果du出嘅數紿終未能夠滿足你要求
你可以把du嘅output再放落python做format,計算
只係用du佢最強項:快讀取
作者: s20012797    時間: 2023-10-19 12:14

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


行到python仲洗等你du做嘅format計算???
作者: epcsub    時間: 2023-10-19 15:09

回復 10 #s20012797
樓主講過, 大folder, python都仲係慢
Python裡 os.lsdir() os.walk() 偏慢
os.scandir()比較快, 但一般都唔夠du快


via HKEPC Reader for Android
作者: s20012797    時間: 2023-10-19 16:14

回復 s20012797
樓主講過, 大folder, python都仲係慢
Python裡 os.lsdir() os.walk() 偏慢
os.scandir()比 ...
epcsub 發表於 2023/10/19 15:09


感覺Python行緊perl的後路,寫係好簡單,但效能跪響度
作者: bongbong3481    時間: 2023-10-19 18:50

回覆 9# epcsub


    真是好快,出到我要既野,而且1行 好簡單既command做好
作者: bongbong3481    時間: 2023-10-19 19:05

感覺Python行緊perl的後路,寫係好簡單,但效能跪響度
s20012797 發表於 2023-10-19 16:14



Python 既速度效能相對其它C++/Java,的確是慢,
但我用開vba ,所以吾覺太大問題,
另外python 開發速度快,用途廣泛,
而且好多source 可以比我抄抄改改,
呢3個優點遮蓋佢既缺點。
作者: epcsub    時間: 2023-10-20 20:27

回復 12 #s20012797

Perl強項集中喺文字及字符處理
Python係喺多個應用領域都有成熟library
只要多人用 隻language就唔死 仲有人會寫module library畀佢
只要under active development 有新技術新 就會整合入去 可以令到隻language 再長命一啲

.NET Java後台比較強大 冇咁易受到「冇咗邊個唔得」嘅問題影響 python perl 好怕chief developer 冇咗道火之後走哂樣
如果可以去走「冇咗邊個唔得」呢個因素 背後嘅團隊管理得好 可以行得遠

via HKEPC Reader for Android
作者: bongbong3481    時間: 2023-10-20 23:12

回覆 15# epcsub


    同意, python 實在太多好用既library,而且好易搵到例子抄抄改改(好少既code 就做到好複雜既野,我做好少改動就用到d code)





歡迎光臨 電腦領域 HKEPC Hardware (https://h2.hkepc.com/forum/) Powered by Discuz! 7.2