我得到了這個腳本并對其進行了一些修改(以避免將相同的檔案提取到一個臨時檔案中)。我有兩個問題:
- 當腳本發現重復時,SourchArchive 總是顯示一個檔案(而不是 2 個包含相同檔案的檔案)
- 當一個壓縮檔案在不同的子檔案夾(在同一個 zip 中)中包含超過 1 個相同的檔案時,腳本會回傳存在重復,這對我不利。如果壓縮檔案有 3 個相同的檔案,則應合并為 1 個檔案,然后將其合并為另一個壓縮檔案
更新:
主要目標是在壓縮檔案之間進行比較,以便在壓縮檔案中找到重復檔案。壓縮檔案可以是 cab 或 zip(zip 可以包含 dll、xml、msi 等。有時它還包含 vip 檔案(vip 是一個壓縮檔案,其中還包含 dll 等檔案))輸出應該是包含相同檔案的壓縮檔案。將結果與 ---------- 分開會很棒
這應該是更大腳本的一部分,如果超過 1 個壓縮檔案中有重復檔案,則該腳本應該停止,因此只有在 $MatchedSourceFiles 有結果時腳本才會停止,否則應該繼續。我希望它現在清楚
Example:
test1.zip contains temp.xml
test2.zip contains temp.xml
The output should be:
SourceArchive DuplicateFile
test1.zip temp.xml
test2.zip temp.xml
------------------------------
The next duplication files
------------------------------
Example 2: (multiple identical files in the same compressed file)
test1.zip contains 2 subfolders
test1.zip contains temp.xml under subfolder1 and also temp.xml under subfolder2
The result should be none
SourceArchive DuplicateFile
Example 3:
test1.zip same as in example 2
test3.zip contains temp.xml
The result should be:
SourceArchive DuplicateFile
test1.zip temp.xml
test3.zip temp.xml
------------------------------
The next duplication files
------------------------------
The next duplication files
------------------------------
Add-Type -AssemblyName System.IO.Compression.FileSystem
$tempFolder = Join-Path -Path ([IO.Path]::GetTempPath()) -ChildPath (New-GUID).Guid
$compressedfiles = Get-ChildItem -Path 'C:\Intel' -Include '*.zip', '*.CAB' -File -Recurse
$MatchedSourceFiles = foreach ($file in $compressedfiles) {
switch ($file.Extension) {
'.zip' {
$t = $tempFolder "\" $file.Name
# the destination folder should NOT already exist here
$null = [System.IO.Compression.ZipFile]::ExtractToDirectory($file.FullName, $t )
try {
Get-ChildItem -Path $tempFolder -Filter '*.vip' -File -Recurse | ForEach-Object {
$null = [System.IO.Compression.ZipFile]::ExtractToDirectory($_.FullName, $t)
}
}
catch {}
}
'.cab' {
# the destination folder MUST exist for expanding .cab files
$null = New-Item -Path $tempFolder -ItemType Directory -Force
expand.exe $file.FullName -F:* $tempFolder > $null
}
}
# now see if there are files with duplicate names
Get-ChildItem -Path $tempFolder -File -Recurse -Exclude vip.manifest, filesSources.txt, *.vip | Group-Object Name |
Where-Object { $_.Count -gt 1 } | ForEach-Object {
foreach ($item in $_.Group) {
# output objects to be collected in $MatchedSourceFiles
[PsCustomObject]@{
SourceArchive = $file.FullName
DuplicateFile = '.{0}' -f $item.FullName.Substring($tempFolder.Length) # relative path
}
}
}
}
# display on screen
$MatchedSourceFiles
$tempFolder | Remove-Item -Force -Recurse
uj5u.com熱心網友回復:
感謝您的示例。使用這些,我將以前的代碼更改為:
Add-Type -AssemblyName System.IO.Compression.FileSystem
$tempFolder = Join-Path -Path ([IO.Path]::GetTempPath()) -ChildPath (New-GUID).Guid
$compressedfiles = Get-ChildItem -Path 'C:\Intel' -Include '*.zip','*.CAB' -File -Recurse
$MatchedSourceFiles = foreach ($file in $compressedfiles) {
switch ($file.Extension) {
'.zip' {
# the destination folder should NOT already exist here
$null = [System.IO.Compression.ZipFile]::ExtractToDirectory($file.FullName, $tempFolder)
# prepare a subfolder name for .vip files
$subTemp = Join-Path -Path $tempFolder -ChildPath ([datetime]::Now.Ticks)
Get-ChildItem -Path $tempFolder -Filter '*.vip' -File -Recurse | ForEach-Object {
$null = [System.IO.Compression.ZipFile]::ExtractToDirectory($_.FullName, $subTemp)
}
}
'.cab' {
# the destination folder MUST exist for expanding .cab files
$null = New-Item -Path $tempFolder -ItemType Directory -Force
expand.exe $file.FullName -F:* $tempFolder > $null
}
}
# output objects for each unique file name in the extracted folder to collect in $MatchedSourceFiles
Get-ChildItem -Path $tempFolder -File -Recurse |
Select-Object @{Name = 'SourceArchive'; Expression = {$file.FullName}},
@{Name = 'FileName'; Expression = {$_.Name}} -Unique
# delete the temporary folder
$tempFolder | Remove-Item -Force -Recurse
}
# at this point $MatchedSourceFiles contains all (unique) filenames from all .zip and/or .cab files
# now see if there are files with duplicate names between the archive files
$result = $MatchedSourceFiles | Group-Object FileName | Where-Object { $_.Count -gt 1 } | ForEach-Object {$_.Group}
# display on screen
$result
# save as CSV file
$result | Export-Csv -Path 'X:\DuplicateFiles.csv' -UseCulture -NoTypeInformation
輸出將是:
示例 1:
SourceArchive FileName
------------- --------
C:\Intel\test1.zip temp.xml
C:\Intel\test2.zip temp.xml
示例 2:
沒有輸出
示例 3:
SourceArchive FileName
------------- --------
C:\Intel\test1.zip temp.xml
C:\Intel\test3.zip temp.xml
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/474186.html
標籤:电源外壳
上一篇:合并2個腳本
