Delete files older than 30 days

Follow

 

#
# The following script is utilized for file cleanup on a given set
# Of defined directories - in this case 4 separate directories.
# This script can be modified to clean up more, or less, locations
# And filter based upon your needs on your server(s).
#


#
# Define todays date
#
$Now = Get-Date

#
# Minimum age files must be to be deleted
#
$Days = "30"

#
# Set our targer folders to remove files from - these can be remote
# directories on Agent machines, or simply UNC paths.
#
$TargetFolder1 = "\\server1\c$\logs"
$TargetFolder2 = "\\server1\c$\dataFiles"
$TargetFolder3 = "\\server2\c$\logs" 
$TargetFolder4 = "\\seixbbgw2\c$\dataFiles"

#
# Delete only certain file extentions - or files with a certain naming convention 
#
$Extension = "*.*"

#
# Define LastWriteTime parameter based on $Days
#
$LastWrite = $Now.AddDays(-$Days)

#
# Check and delete any files in the first defined location
#
$Files1 = Get-Childitem $TargetFolder1 -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}

foreach ($File in $Files1)
{
    if ($File -ne $NULL)
    {
        Write-Host "Deleting File $File"
        Remove-Item $File.FullName | out-null
    }
}

#
# Check and delete any files in the second defined location
#
$Files2 = Get-Childitem $TargetFolder2 -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}

foreach ($File in $Files2)
{
    if ($File -ne $NULL)
    {
        Write-Host "Deleting File $File"
        Remove-Item $File.FullName | out-null
    }
}    

#
# Check and delete any files in the third defined location
#
$Files3 = Get-Childitem $TargetFolder3 -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}

foreach ($File in $Files3)
{
    if ($File -ne $NULL)
    {
        Write-Host "Deleting File $File"
        Remove-Item $File.FullName | out-null
    }
}    
        
#
# Check and delete any files in the fourth defined location
#
$Files4 = Get-Childitem $TargetFolder4 -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}

foreach ($File in $Files4)
{
    if ($File -ne $NULL)
    {
        Write-Host "Deleting File $File"
        Remove-Item $File.FullName | out-null
    }
}

 

Have more questions? Submit a request

Comments

  • Avatar
    Arden Walde

    Pardon my disappointment - but this is just a Powershell script with many similar examples provided through a simple Internet search. Please update this article to show how it can be specifically incorporated into a JAMS job... things I would like to know are:

    1) how would I submit this job with a specific folder or selection of folders to apply this job to; your script defines 4 hard-coded locations whereas I may want to start this job with an entry parameter (or 4?) for the job to run

    2) uses the Powershell 'Write-Host" verb to output to the console; I am aware that JAMS does somehow capture console output but where would I find that? or capture it to include the list of files in an email? or a table if I were keeping a log table of deletions.

    Thanks for providing and keeping this knowledgebase current.

  • Avatar
    Gennaro Piccolo

    Hello Arden, if you have specific questions regarding a JAMS job please open a support ticket using support@jamsscheduler.com. It is much easier to track and serve you there. It is difficult to provide examples for every customers use case, and so, this is a basic example that someone might want to use.

    If you'd like more information on parameter and variable usage in jobs, please see this documentation link:
    https://www.jamsscheduler.com/doc/JAMSHelp/Default.html#ParsingParametersandVariables.html

    Write-Host will write to the console which is captured by standard out, which would be in the JAMS job log.

    If you already have written a Powershell script that performs your use cases, using write-host will capture that information in the JAMS job log.