PowerShell无限循环删除Emby转码临时文件失效问题排查及解决方案咨询
Hey there, let's break down why your infinite loop script isn't deleting those old transcode files, and get you a reliable solution!
The Root Cause of the Failed Loop
Your problem comes down to where you're calculating the $limit variable. Right now, you set $limit = (Get-Date).AddMinutes(-5) outside the while loop. That means this timestamp is only calculated once—when you first launch the script.
For example:
- If you start the script at 10:00 AM,
$limitgets set to 9:55 AM. - A new transcode file created at 10:02 AM won't be "older than 5 minutes" until 10:07 AM. But your script is still using the original 9:55 AM threshold, so 10:02 AM is newer than that cutoff, and the file never gets flagged for deletion.
The loop keeps running, but it's always checking against that fixed initial timestamp instead of updating it each time.
Fixes to Keep Your Transcode Folder Clean
Solution 1: Update the Limit Inside the Loop
The quick fix is to move the $limit calculation inside the while loop, so it refreshes every time the loop runs. We'll also add a small delay to avoid spamming your CPU with constant checks:
$path = "C:\Users\*myuser*\AppData\Roaming\Emby-Server\programdata\transcoding-temp" while($true){ # Calculate the 5-minute threshold fresh every loop iteration $limit = (Get-Date).AddMinutes(-5) Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force # Wait 30 seconds before checking again (adjust this value as needed) Start-Sleep -Seconds 30 }
This way, every time the loop runs, it uses the current time minus 5 minutes as the cutoff, so any file older than that will get deleted.
Solution 2: Use Windows Task Scheduler (Recommended)
Infinite loops aren't the most reliable long-term fix—if the PowerShell window closes or your PC restarts, the cleanup stops. A better approach is to use Windows Task Scheduler to run your working one-time script on a regular interval:
- Save your original working script (without the loop) as a
.ps1file, e.g.,CleanEmbyTranscode.ps1:$limit = (Get-Date).AddMinutes(-5) $path = "C:\Users\*myuser*\AppData\Roaming\Emby-Server\programdata\transcoding-temp" Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force - Open Task Scheduler and create a new basic task:
- Name it something like "Clean Emby Transcode Temp Files"
- Set a trigger: Choose "Daily", then check "Repeat task every" and select 5 minutes, with a duration of "Indefinitely"
- Set an action: "Start a program", then enter
powershell.exeas the program/script, and add this parameter (replace the path with your actual script path):-ExecutionPolicy Bypass -File "C:\Path\To\Your\CleanEmbyTranscode.ps1" - Finish the wizard, and make sure to enable the task.
This setup will automatically run the cleanup every 5 minutes, even if you log out or restart your PC (just set the task to run whether you're logged in or not).
内容的提问来源于stack exchange,提问作者Audiosleef

