-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_zip_files
More file actions
89 lines (72 loc) · 3.02 KB
/
check_zip_files
File metadata and controls
89 lines (72 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Set the root directory containing your zip files
$rootDir = "PATH/TO/ZIPS"
# Hashtable to store results keyed by the parent zip file name
$zipResults = @{}
# Function to recursively check zip files and directories
function Check-ZipFiles {
param (
[Parameter(Mandatory=$true)]
[string]$directory,
[string]$parentZipName = ""
)
# Get all files and directories in the current directory
$items = Get-ChildItem -Path $directory
foreach ($item in $items) {
if ($item.PSIsContainer) {
# If the item is a directory, recurse into it
Check-ZipFiles -directory $item.FullName -parentZipName $parentZipName
}
elseif ($item.Extension -ieq '.zip') {
# If the item is a zip file, process it
# Check if this zip file contains the specified extension
$containsTarget = $false
try {
$zip = [System.IO.Compression.ZipFile]::OpenRead($item.FullName)
foreach ($entry in $zip.Entries) {
if ($entry.FullName -match '\.paf$|\.cdi$|\.scm$|\.col$|\.sel$|\.asc$') {
$containsTarget = $true
}
}
$zip.Dispose()
# If either extension is found, record the result tied to the parent zip file
$actualParentZip = if ($parentZipName -ne "") { $parentZipName } else { $item.Name }
if ($containsTarget) {
if (-not $zipResults.ContainsKey($actualParentZip)) {
$zipResults[$actualParentZip] = @{
Extensions = @()
}
}
if ($containsTarget -and -not $zipResults[$actualParentZip].Extensions.Contains("Target")) {
$zipResults[$actualParentZip].Extensions += "Target"
}
}
}
catch {
Write-Host "Error processing $($item.Name): $_"
}
# Extract the zip file to a temporary directory
$tempDir = Join-Path $directory ($item.BaseName + "_temp")
Expand-Archive -Path $item.FullName -DestinationPath $tempDir -Force
# Recursively check the extracted contents
if (Test-Path $tempDir -PathType Container) {
Check-ZipFiles -directory $tempDir -parentZipName $actualParentZip
}
# Clean up the temporary directory
Remove-Item -Path $tempDir -Recurse -Force
}
}
}
# Start checking from the root directory
Check-ZipFiles -directory $rootDir
# Output and sort results
$sortedResults = $zipResults.GetEnumerator() | Sort-Object {
if ($_ -match '108-(\d{5})\.\w\.zip$') { [int]$matches[1] } else { 0 }
} -Descending
foreach ($result in $sortedResults) {
$parentZip = $result.Key
$extensions = $result.Value.Extensions -join ", "
if ($extensions) {
Write-Host "$parentZip"
}
}
Write-Host "Finished checking zip files."