Files
SpotX/Cache/cache-spotify.ps1
amd64fox 9c22f6d34d Cache cleanup optimization
- Fixed slow client startup if cache purge was set
- Now there is a log file (Spotify/cache/log.txt) in which you can find information about the script to clear the cache.
- A little code optimization
2022-02-05 22:43:40 +03:00

43 lines
1.7 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<#
Name: Clear Spotify Cache.
Description: The script clears outdated cache from the listened music in Spotify.
Fires every time you completely close the client (If the client was minimized to tray then the script will not work).
For the APPDATA\Spotify\Data folder, the rule is that all cache files that are not used
by the customer more than the specified number of days will be deleted.
#>
$day = 7 # Number of days after which the cache is considered stale
# Clear the \Data folder if it finds an outdated cache
try {
If (!(Test-Path -Path $env:LOCALAPPDATA\Spotify\Data)) {
"$(Get-Date -uformat %D %T) Folder Local\Spotify\Data not found" | Out-File log.txt -append
exit
}
$check = Get-ChildItem $env:LOCALAPPDATA\Spotify\Data -File -Recurse | Where-Object lastaccesstime -lt (get-date).AddDays(-$day)
if ($check.Length -ge 1) {
$count = $check
$sum = $count | Measure-Object -Property Length -sum
if ($sum.Sum -ge 104434441824) {
$gb = "{0:N2} Gb" -f (($check | Measure-Object Length -s).sum / 1Gb)
"$(Get-Date -uformat %D %T) Removed $gb obsolete cache" | Out-File log.txt -append
}
else {
$mb = "{0:N2} Mb" -f (($check | Measure-Object Length -s).sum / 1Mb)
"$(Get-Date -uformat %D %T) Removed $mb obsolete cache" | Out-File log.txt -append
}
Get-ChildItem $env:LOCALAPPDATA\Spotify\Data -File -Recurse | Where-Object lastaccesstime -lt (get-date).AddDays(-$day) | Remove-Item
}
if ($check.Length -lt 1) {
"$(Get-Date -uformat %D %T) Stale cache not found" | Out-File log.txt -append
}
}
catch {
"$(Get-Date -uformat %D %T) $error[0].Exception" | Out-File log.txt -append
}
exit