Skip to content

Instantly share code, notes, and snippets.

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 neodigm/7d816c746c0c18a61923126606baf7a9 to your computer and use it in GitHub Desktop.
Save neodigm/7d816c746c0c18a61923126606baf7a9 to your computer and use it in GitHub Desktop.
This PowerShell script will remove files, if they are older than one hour and if the Scheduled task is NOT running, and if the Scheduled task has not updated it log in the past two minutes.
# 20160601 Scott C. Krause
# This PowerShell script will remove files, if they are older than one hour and if the Scheduled task is NOT running, and if the Scheduled task has not updated it log in the past two minutes.
Set-ExecutionPolicy Bypass
# popu var if compr proc is not busy
$recent_logged = Get-Item C:\ltd_auto_image\logs\ltd_auto_image.txt | where-object {(new-timespan $_.LastWriteTime).minutes -ge 16}
if ($recent_logged) {
# if sched task / compr proc is state running del
$ltd_auto_running = Get-ScheduledTask -TaskName ltd_auto_image | Where State -eq "Running"
#if ($recent_logged) {
$myFolder = 'C:\ltd_auto_image\images_product\images_in\'
$filesToDelete = Get-ChildItem -Path $myFolder | Where-Object {$_.PSIsContainer -eq $false -and $_.LastWriteTime -lt $(Get-Date).AddHours(-1)}
$filesToDelete | Remove-Item -Force
$myFolder_s = 'C:\ltd_auto_image\images_static\images_in\'
$filesToDelete_s = Get-ChildItem -Path $myFolder_s | Where-Object {$_.PSIsContainer -eq $false -and $_.LastWriteTime -lt $(Get-Date).AddHours(-1)}
$filesToDelete_s | Remove-Item -Force
#} Else {
# Exec failsafe
#}
}
exit
@neodigm
Copy link
Author

neodigm commented Aug 22, 2016

20160601 Scott C. Krause
This PowerShell script will remove files, if they are older than one hour and if the Scheduled task is NOT running, and if the Scheduled task has not updated it log in the past two minutes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment