Skip to content

Instantly share code, notes, and snippets.

@2good4hisowngood
Last active April 6, 2024 18:32
Show Gist options
  • Save 2good4hisowngood/ee1049d3c6a0249381845304d2e018c7 to your computer and use it in GitHub Desktop.
Save 2good4hisowngood/ee1049d3c6a0249381845304d2e018c7 to your computer and use it in GitHub Desktop.
Script I put together to archive relevant files from old windows drives before destroying them.
# Define file types by category
$documentExtensions = @('.doc', '.docx', '.odt', '.rtf', '.txt', '.pdf', '.pages', '.xls', '.xlsx', '.ods', '.csv', '.ppt', '.pptx', '.odp', '.key', '.one', '.enex')
$photoExtensions = @('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tif', '.tiff', '.raw', '.cr2', '.nef', '.orf', '.sr2')
$videoExtensions = @('.mp4', '.mov', '.avi', '.wmv', '.mkv', '.flv', '.mpeg', '.mpg', '.hevc', '.h265', '.m2ts', '.vob')
$musicExtensions = @('.mp3', '.wav', '.aac', '.flac', '.ogg', '.m4a', '.wma', '.ape', '.alac')
# Combine all extensions for easier processing
$allExtensions = $documentExtensions + $photoExtensions + $videoExtensions + $musicExtensions
# Define directories to exclude
$excludedDirs = @('Windows', 'Program Files', 'System Volume Information', '.Trash') # Update as needed
# Paths
$startPath = 'D:\' # Change to your starting directory
$destinationRoot = 'F:\Backups\random_hdd' # Set your destination root directory
# Script block to filter out excluded directories
$filterScriptBlock = {
param($path)
$excluded = $false
foreach ($exDir in $GLOBALS:excludedDirs) {
if ($path -like "*\$exDir*") {
$excluded = $true
break
}
}
return -not $excluded
}
# Find and copy files
Get-ChildItem -Path $startPath -Recurse -File | Where-Object {
$file = $_
$extension = $file.Extension
$allExtensions -contains $extension -and (& $filterScriptBlock $file.DirectoryName)
} | ForEach-Object {
$file = $_
$relativePath = $file.DirectoryName.Replace($startPath, '').TrimStart('\')
$destinationPath = Join-Path -Path $destinationRoot -ChildPath $relativePath
# Create destination directory if it doesn't exist
if (-not (Test-Path -Path $destinationPath -PathType Container)) {
New-Item -ItemType Directory -Path $destinationPath | Out-Null
}
$destinationFile = Join-Path -Path $destinationPath -ChildPath $file.Name
Try {
Copy-Item -Path $file.FullName -Destination $destinationFile
Write-Output "Copied: $($file.FullName) to $destinationFile"
} Catch {
Write-Warning "Failed to copy $($file.FullName) to $destinationFile : $_"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment