我有两个 CSV 文件,其中包含相同文件夹的 FileCount 和 Foldersize,它们是在不同时间获取的。(相同的标题、相同的行和列。这两个文件之间的唯一区别是文件计数和文件夹大小值)
我需要比较两者,并报告文件数量变化最大的子文件夹和文件夹大小变化最大的子文件夹。
下面的 CSV 摘录。
CSV1
Directory FileCount FolderSize
D:\test\Documents 61 18.75
D:\test\Media 61 67,488.43
D:\test\Photos 53 7.88
CSV2
Directory FileCount FolderSize
D:\test\Documents 61 18.75
D:\test\Media 59 62,192.40
D:\test\Photos 32 6.51
到目前为止,我有下面的内容,但它输出的是 CSV1 的精确副本,而没有进行我在 do 循环中所做的更改。我做错了什么?
#Importing CSV files
$CSV1 = Import-Csv -Path D:\test\folderstat1.csv
$CSV2 = Import-Csv -Path D:\test\folderstat2.csv
#Setting counts for do loop
$end = $csv1.Count
$count = 0
#Set $csv1 Filecount and foldersize to the difference between the row in CSV1 #and CSV2
do{
$csv1.filecount[$count] = $csv1.filecount[$count] - $csv2.filecount[$count]
$csv1.FolderSize[$count] = $csv1.FolderSize[$count] - $csv2.FolderSize[$count]
$count++
}until($count -eq $end)
$CSV1 | export-csv D:\test\out.csv -NoTypeInformation
答案1
您错误地放置了索引。请尝试:
$csv1[$count].filecount -= $csv2[$count].filecount
$csv1[$count].FolderSize-= $csv2[$count].FolderSize
或者创建一个新的 CSV3
## Q:\Test\2018\06\01\SU_1327671.ps1
#Importing CSV files
$CSV1 = Import-Csv -Path "D:\test\folderstat1.csv"
$CSV2 = Import-Csv -Path "D:\test\folderstat2.csv"
#Setting counts for do loop
$end = $csv1.Count
$count = 0
#Set $CSV3 Filecount and foldersize to the difference between the row in CSV1 #and CSV2
$CSV3 = do{
[PSCustomObject]@{
Directory = $csv1[$count].Directory
Filecount = ($csv1[$count].filecount - $csv2[$count].filecount)
FolderSize= ($csv1[$count].FolderSize - $csv2[$count].FolderSize)
}
$count++
} until($count -eq $end)
$CSV3 | Export-Csv "D:\test\out.csv" -NoTypeInformation
"=" * 50
$CSV3 | Sort {[Math]::Abs($_.FolderSize)} -Desc | Select -First 1|
ForEach{"Largest (absolute) change {0} in {1}" -f $_.FolderSize,$_.Directory}
示例输出:
> . .\SU_1327671.ps1
==================================================
Largest (absolute) change 5296 in D:\test\Media
> import-csv .\out.csv
Directory Filecount FolderSize
--------- --------- ----------
D:\test\Documents 0 0
D:\test\Media 2 5296
D:\test\Photos 21 1,37