
我有一個充滿備份的硬碟,我需要按照以下特定方式快速在電子表格上詳細說明這些備份。我希望有一個簡單的方法可以透過某種 .bat 或 .cmd 檔案來完成我需要的操作,但我不知道從哪裡開始。
我所需要的只是層次結構中的前兩個資料夾層級以轉到 csv:
這是我需要從驅動器導出的數據:
我嘗試在powershell中使用以下程式碼:
cd\ c:
<--- 我的測試資料夾所在的位置
然後
cd\ test
<--- 包含我要輸出的測試資料夾結構
那麼這個我成立在發布我的問題之前。
Get-ChildItem -Recurse -Directory -Depth 3 |
Select-Object FullName |
Export-Csv Test.csv -NoTypeInformation
我意識到我需要使用Select-Object
指定更多資料夾屬性來輸出到 csv,但我似乎無法讓上述內容以它應有的基本方式運作。
相反,我收到以下錯誤:
Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.
At line:1 char:34
+ Get-ChildItem -Recurse -Directory <<<< -Depth 3 |
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
我還嘗試將我的測試資料夾匯入到 Notepad++ 中,我可以將其視為可以瀏覽的工作區,但無法使用我需要的資訊匯出到 csv。我能看到的唯一選項是當我右鍵單擊目錄時在螢幕截圖中看到的內容,並且沒有輸出選項(我不是這方面的專家)。
我嘗試的另一件事是創建一個 test.cmd 文件,其中包含以下內容:
C:\TEST\
dir >> C:\TEST\test.csv
運行此命令後,它似乎讓我更接近我正在尋找的內容,但我仍然沒有看到我原始文件中指定的所需信息excel截圖在這篇文章的頂部:
正如您所看到的,我缺少正確的目錄路徑,而我所看到的<DIR>
只是,我也看不到包含子資料夾的資料夾結構中的第二級。每個資料夾的檔案數量和大小以及列的排序也不正確。
任何具有基於命令的資料庫創建知識的人都可以幫助我嗎?我的問題被擱置,因為它不夠詳細,但我現在已盡最大努力來解釋我嘗試過的所有內容,並且通過從我們的近線存儲將超過 2 PB 的存儲備份到電子表格,我需要能夠快速有效地做到這一點。
提前謝謝了。
答案1
我同意 Bill_Stuart 關於 WinDirStat 協議的觀點...(儘管我已經很多年不需要使用它了)但是,如果你說必須,那麼也許像下面這樣,當然將計算更改為你選擇的任何內容尺寸的事情。
Clear-Host
Get-ChildItem -Path 'd:\temp' -Recurse -Directory -Depth 3 |
Select-Object Name,FullName,
@{Name='FolderCount';Expression = {(Get-ChildItem -Path $_.FullName -Directory -Recurse | Measure-Object).Count}},
@{Name='FileCount';Expression = {(Get-ChildItem -Path $_.FullName -File -Recurse| Measure-Object).Count}},
@{Name='SizeMB';Expression = {(Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB}} `
| Format-Table -AutoSize
# Results
Name FullName FolderCount FileCount SizeMB
---- -------- ----------- --------- ------
abcpath0 D:\temp\abcpath0 2 5 0.005889892578125
abcpath1 D:\temp\abcpath1 2 5 0.005889892578125
abcpath2 D:\temp\abcpath2 2 5 0.005889892578125
Duplicates D:\temp\Duplicates 24 12677 88.9826745986938
EmptyFolder D:\temp\EmptyFolder 0 0 0
NewFiles D:\temp\NewFiles 0 4 0.000568389892578125
...
答案2
嘗試在 Powershell 中使用以下內容。
$dir = "C:\"
$results = @()
gci $dir -Recurse -Depth 1 | % {
$temp = [ordered]@{
NAME = $_
SIZE = "{0:N2} MB" -f ((gci $_.Fullname -Recurse | measure -Property Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB)
FILE_COUNT = (gci -File $_.FullName -Recurse | measure | select -ExpandProperty Count)
FOLDER_COUNT = (gci -Directory $_.FullName -Recurse | measure | select -ExpandProperty Count)
DIRECTORY_PATH = $_.Fullname
}
$results += New-Object PSObject -Property $temp
}
$results | export-csv -Path "C:\output.csv" -NoTypeInformation