Skip to content

Instantly share code, notes, and snippets.

@quonic
Created February 14, 2019 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quonic/4dc364bb9de62ec0bbc6d18169bb920f to your computer and use it in GitHub Desktop.
Save quonic/4dc364bb9de62ec0bbc6d18169bb920f to your computer and use it in GitHub Desktop.
Some example code to rename discord cached images
# Function to figure out what the file type is
function Get-ImageExt {
[CmdletBinding()]
# [OutputType([System.Boolean])]
param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[Alias('PSPath')]
[string] $Path
)
PROCESS {
$knownHeaders = @{
jpg = @( "FF", "D8" );
bmp = @( "42", "4D" );
gif = @( "47", "49", "46" );
tif = @( "49", "49", "2A" );
png = @( "89", "50", "4E", "47", "0D", "0A", "1A", "0A" );
pdf = @( "25", "50", "44", "46" );
}
# coerce relative paths from the pipeline into full paths
if ($_ -ne $null) {
$Path = $_.FullName
}
# read in the first 8 bits
$bytes = Get-Content -LiteralPath $Path -Encoding Byte -ReadCount 1 -TotalCount 8 -ErrorAction Ignore
$retval = $false
foreach ($key in $knownHeaders.Keys) {
# make the file header data the same length and format as the known header
$fileHeader = $bytes |
Select-Object -First $knownHeaders[$key].Length |
ForEach-Object { $_.ToString("X2") }
if ($fileHeader.Length -eq 0) {
continue
}
# compare the two headers
$diff = Compare-Object -ReferenceObject $knownHeaders[$key] -DifferenceObject $fileHeader
if (($diff | Measure-Object).Count -eq 0) {
$diff
$retval = $true
}
}
switch ($knownHeaders[$key].Values) {
$knownHeaders[0].Values { return "jpg" }
$knownHeaders[1].Values { return "bmp" }
$knownHeaders[2].Values { return "gif" }
$knownHeaders[3].Values { return "tif" }
$knownHeaders[4].Values { return "png" }
$knownHeaders[5].Values { return "pdf" }
Default {}
}
return $retval
}
}
# Rename Discord images with extensions
Get-Item "$($env:APPDATA)\discord\Cache\f_*" | ForEach-Object {
$ext = Get-ImageExt -Path $_
Rename-Item -Path $_ -NewName "$_.$ext"
}
# Remove Discord cache extentions
Get-Item "$($env:APPDATA)\discord\Cache\f_*" | ForEach-Object {
Rename-Item -Path $_ -NewName $_.BaseName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment