The attached powershell script searches all files with extension .gif in the specified folder (including its subfolders), examines the file by checking the file header and appends the correct extension for the file content found.
The file formats JPG, PNG, WebM and WebP are recognized.
Created
June 13, 2024 23:06
-
-
Save thomas694/cee620319cade7c59a85f841e29a6648 to your computer and use it in GitHub Desktop.
Correct GIF file extensions in a folder tree
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
| if (!$args[0]) { | |
| Write-Host "No Folder specified!" | |
| Exit | |
| } | |
| $path = $args[0] | |
| $riff = 0x52,0x49,0x46,0x46 | |
| $webm = 0x1A,0x45,0xDF,0xA3 | |
| $jpg = 0xFF,0xD8,0xFF | |
| $png = 0x89,0x50,0x4E,0x47 | |
| foreach ($item in Get-ChildItem $path -Recurse -Filter *.gif) | |
| { | |
| $fn = $item.FullName | |
| $bytes = Get-Content -LiteralPath $fn -Encoding byte -TotalCount 4 | |
| if (($bytes[0] -eq $riff[0]) -and ($bytes[1] -eq $riff[1]) -and ($bytes[2] -eq $riff[2]) -and ($bytes[3] -eq $riff[3])) { | |
| $newName = "$fn.webp" | |
| Rename-Item -Path $fn -NewName $newName | |
| Write-Host "$fn.webp" | |
| } | |
| if (($bytes[0] -eq $webm[0]) -and ($bytes[1] -eq $webm[1]) -and ($bytes[2] -eq $webm[2]) -and ($bytes[3] -eq $webm[3])) { | |
| $newName = "$fn.webm" | |
| Rename-Item -Path $fn -NewName $newName | |
| Write-Host "$fn.webm" | |
| } | |
| if (($bytes[0] -eq $jpg[0]) -and ($bytes[1] -eq $jpg[1]) -and ($bytes[2] -eq $jpg[2])) { | |
| $newName = "$fn.jpg" | |
| Rename-Item -LiteralPath $fn -NewName $newName | |
| Write-Host "$fn.jpg" | |
| } | |
| if (($bytes[0] -eq $png[0]) -and ($bytes[1] -eq $png[1]) -and ($bytes[2] -eq $png[2]) -and ($bytes[3] -eq $png[3])) { | |
| $newName = "$fn.png" | |
| Rename-Item -LiteralPath $fn -NewName $newName | |
| Write-Host "$fn.png" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment