Created
April 3, 2026 09:34
-
-
Save pleabargain/d409f3dde5c2bd3fff527bf8fbdfce50 to your computer and use it in GitHub Desktop.
powershell extract files and move the ZIP files to a separate folder after parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Get the location where the script is currently running | |
| $currentDir = $PSScriptRoot | |
| if ([string]::IsNullOrEmpty($currentDir)) { $currentDir = Get-Location } | |
| $processedPath = Join-Path $currentDir "Processed" | |
| # Counters | |
| $extractedCount = 0 | |
| $duplicateCount = 0 | |
| $verifiedZips = @() | |
| # Ensure Processed folder exists | |
| if (-not (Test-Path $processedPath)) { | |
| New-Item -Path $processedPath -ItemType Directory | Out-Null | |
| } | |
| # Find all zip files recursively starting from script location | |
| $zipFiles = Get-ChildItem -Path $currentDir -Filter "*.zip" -Recurse | |
| foreach ($zip in $zipFiles) { | |
| # Skip if zip is already inside the Processed folder | |
| if ($zip.FullName -like "$processedPath*") { continue } | |
| Write-Host "Processing: $($zip.Name)" -ForegroundColor Cyan | |
| $shell = New-Object -ComObject Shell.Application | |
| $zipArchive = $shell.NameSpace($zip.FullName) | |
| # Extract items | |
| foreach ($item in $zipArchive.Items()) { | |
| $targetFile = Join-Path $currentDir $item.Name | |
| if (-not (Test-Path $targetFile)) { | |
| $shell.NameSpace($currentDir).CopyHere($item, 16) | |
| $extractedCount++ | |
| } else { | |
| $duplicateCount++ | |
| } | |
| } | |
| # Move the Zip | |
| $destination = Join-Path $processedPath $zip.Name | |
| Move-Item -Path $zip.FullName -Destination $destination -Force | |
| # Verification Step: Check if the move was successful | |
| if (Test-Path $destination) { | |
| $verifiedZips += $zip.Name | |
| } else { | |
| Write-Host " Warning: Failed to verify move for $($zip.Name)" -ForegroundColor Red | |
| } | |
| } | |
| # --- Summary Report --- | |
| Write-Host "`n" + ("=" * 40) -ForegroundColor White | |
| Write-Host " FINAL EXTRACTION REPORT" -ForegroundColor White | |
| Write-Host ("=" * 40) -ForegroundColor White | |
| Write-Host "Total Images Extracted : $extractedCount" -ForegroundColor Green | |
| Write-Host "Duplicates Skipped : $duplicateCount" -ForegroundColor Yellow | |
| Write-Host ("-" * 40) -ForegroundColor White | |
| if ($verifiedZips.Count -gt 0) { | |
| Write-Host "The following ZIPs were VERIFIED moved to '$processedPath':" -ForegroundColor Cyan | |
| foreach ($name in $verifiedZips) { | |
| Write-Host " [OK] $name" | |
| } | |
| Write-Host "`nThese files are now safe to delete." -ForegroundColor Green | |
| } else { | |
| Write-Host "No new ZIP files were processed." -ForegroundColor Gray | |
| } | |
| Write-Host ("=" * 40) -ForegroundColor White |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment