![]() |
I AM THE FUNNY!!! |
---|
Ok, so I had to create a script recently that deletes all files in specific locations that are older than a time limit (14days in this instance) and then clean
up all the empty folders.
But to test, firstly you need to create files and folders
<# .SYNOPSIS <A brief description of the script> .DESCRIPTION <A detailed description of the script> .PARAMETER <paramName> <Description of script parameter> .EXAMPLE <An example of using the script> #> New-Item "C:\data\A\Shared Transfer\Yes" -type directory New-Item "C:\data\B\Shared Transfer\No" -type directory New-Item "C:\data\C\Shared Transfer\Maybe" -type directory New-Item "C:\data\D\Shared Transfer\qwerty" -type directory New-Item "C:\data\E\Shared Transfer\asdf" -type directory New-Item "C:\data\F\Shared Transfer\zxcv" -type directory New-Item "C:\data\G\Shared Transfer\hi" -type directory New-Item "C:\data\H\Shared Transfer\bye" -type directory New-Item "C:\data\A\Shared Transfer\potato" -type directory New-Item "C:\data\B\Shared Transfer\potato" -type directory New-Item "C:\data\C\Shared Transfer\potato" -type directory New-Item "C:\data\D\Shared Transfer\potato" -type directory New-Item "C:\data\E\Shared Transfer\potato" -type directory New-Item "C:\data\F\Shared Transfer\potato" -type directory New-Item "C:\data\G\Shared Transfer\potato" -type directory New-Item "C:\data\H\Shared Transfer\potato" -type directory New-Item "C:\data\A\Shared Transfer\jkl" -type directory New-Item "C:\data\B\Shared Transfer\jkl" -type directory New-Item "C:\data\C\Shared Transfer\jkl" -type directory New-Item "C:\data\D\Shared Transfer\jkl" -type directory New-Item "C:\data\E\Shared Transfer\jkl" -type directory New-Item "C:\data\F\Shared Transfer\jkl" -type directory New-Item "C:\data\G\Shared Transfer\jkl" -type directory New-Item "C:\data\H\Shared Transfer\jkl" -type directory New-Item "C:\data\A\Shared Transfer\blah.txt" -type file New-Item "C:\data\B\Shared Transfer\blah.txt" -type file New-Item "C:\data\C\Shared Transfer\blah.txt" -type file New-Item "C:\data\D\Shared Transfer\blah.txt" -type file New-Item "C:\data\E\Shared Transfer\blah.txt" -type file New-Item "C:\data\F\Shared Transfer\blah.txt" -type file New-Item "C:\data\G\Shared Transfer\blah.txt" -type file New-Item "C:\data\H\Shared Transfer\blah.txt" -type file New-Item "C:\data\A\Shared Transfer\!!README!!.txt" -type file New-Item "C:\data\B\Shared Transfer\!!README!!.txt" -type file New-Item "C:\data\C\Shared Transfer\!!README!!.txt" -type file New-Item "C:\data\D\Shared Transfer\!!README!!.txt" -type file New-Item "C:\data\E\Shared Transfer\!!README!!.txt" -type file New-Item "C:\data\F\Shared Transfer\!!README!!.txt" -type file New-Item "C:\data\G\Shared Transfer\!!README!!.txt" -type file New-Item "C:\data\H\Shared Transfer\!!README!!.txt" -type file New-Item "C:\data\A\Shared Transfer\!!This Location Will Be Cleared Daily Files And Folders Older than 14 days will be removed.txt" -type file New-Item "C:\data\B\Shared Transfer\!!This Location Will Be Cleared Daily Files And Folders Older than 14 days will be removed.txt" -type file New-Item "C:\data\C\Shared Transfer\!!This Location Will Be Cleared Daily Files And Folders Older than 14 days will be removed.txt" -type file New-Item "C:\data\D\Shared Transfer\!!This Location Will Be Cleared Daily Files And Folders Older than 14 days will be removed.txt" -type file New-Item "C:\data\E\Shared Transfer\!!This Location Will Be Cleared Daily Files And Folders Older than 14 days will be removed.txt" -type file New-Item "C:\data\F\Shared Transfer\!!This Location Will Be Cleared Daily Files And Folders Older than 14 days will be removed.txt" -type file New-Item "C:\data\G\Shared Transfer\!!This Location Will Be Cleared Daily Files And Folders Older than 14 days will be removed.txt" -type file New-Item "C:\data\H\Shared Transfer\!!This Location Will Be Cleared Daily Files And Folders Older than 14 days will be removed.txt" -type file start-sleep -s 2 New-Item "C:\data\A\Shared Transfer\Yes\a" -type directory New-Item "C:\data\B\Shared Transfer\No\b" -type directory New-Item "C:\data\C\Shared Transfer\Maybe\c" -type directory New-Item "C:\data\D\Shared Transfer\qwerty\d" -type directory New-Item "C:\data\E\Shared Transfer\asdf\e" -type directory New-Item "C:\data\F\Shared Transfer\zxcv\f" -type directory New-Item "C:\data\G\Shared Transfer\hi\g" -type directory New-Item "C:\data\H\Shared Transfer\bye\h" -type directory New-Item "C:\data\A\Shared Transfer\Yes\blaht.txt" -type file New-Item "C:\data\B\Shared Transfer\No\blaht.txt" -type file New-Item "C:\data\C\Shared Transfer\Maybe\blaht.txt" -type file New-Item "C:\data\D\Shared Transfer\qwerty\blaht.txt" -type file New-Item "C:\data\E\Shared Transfer\asdf\blaht.txt" -type file New-Item "C:\data\F\Shared Transfer\zxcv\blaht.txt" -type file New-Item "C:\data\G\Shared Transfer\hi\blaht.txt" -type file New-Item "C:\data\H\Shared Transfer\bye\blaht.txt" -type file start-sleep -s 2 New-Item "C:\data\A\Shared Transfer\Yes\a\dfg.docx" -type file New-Item "C:\data\B\Shared Transfer\No\b\meh.docx" -type file New-Item "C:\data\C\Shared Transfer\Maybe\c\asd.docx" -type file New-Item "C:\data\D\Shared Transfer\qwerty\d\asdf.docx" -type file New-Item "C:\data\E\Shared Transfer\asdf\e\tyu.docx" -type file New-Item "C:\data\F\Shared Transfer\zxcv\f\fghjk.docx" -type file New-Item "C:\data\G\Shared Transfer\hi\g\hk.docx" -type file New-Item "C:\data\H\Shared Transfer\bye\h\eh.docx" -type file
So you can set the creation date of files doing something like the below to make it older than 14 days. Or just change it to 0 days.
Get-ChildItem 'C:\data\A\Shared Transfer\blah.txt' | % { $_.CreationTime = '01/10/2008 1:00' }
Anyway, deletion script is below.
<# .SYNOPSIS Deleting files that are older then a certain time period There is a list of exclusions files and folders .DESCRIPTION This script has been created to delete files and which are greater than 14 days old and remove empty folders .PARAMETER - $filesToSave List of files that will be kept in the directory - $folderlocations locations that the script will be run - $timeLimit Sets how old the files have to be before they are deleted by the script. #> # Name of files which should not be deleted $filesToSave = @("!!README!!.txt","!!This Location Will Be Cleared Daily Files And Folders Older than 14 days will be removed.txt") # List of folder locations to delete from $folderlocations = @("C:\data\A\Shared Transfer","C:\data\B\Shared Transfer","C:\data\C\Shared Transfer","C:\data\D\Shared Transfer","C:\data\E\Shared Transfer","C:\data\F\Shared Transfer","C:\data\G\Shared Transfer","C:\data\H\Shared Transfer") # time limit to delete files $timeLimit = (Get-Date).AddDays(14) cls Function DeleteFileAndFolders([String]$folder){ #recursion has been set up so it will go through the all of the folders and delete the ones which have been emptied after the files have been deleted foreach ($childDirectory in Get-ChildItem -Force -LiteralPath $folder -Directory) { DeleteFileAndFolders $childDirectory.FullName } $currentChildren = Get-ChildItem -Force -LiteralPath $folder #Gets all currently empty folders $isEmpty = $currentChildren -eq $null #deletes all currently empty folders if ($isEmpty) { Write-Verbose "Removing empty folder at path '${folder}'." -Verbose Remove-Item -Force -LiteralPath $folder }Else{ #Deletes files older than the set time limit foreach ($file in $currentChildren){ If ($file.CreationTime -lt $timeLimit){ if(!$file.PSIsContainer -and $filesToSave -contains $file -eq $false){ write-host "Deleting file $file" -foreground "magenta" Remove-Item -exclude $filesToSave -literalpath $file.fullname -Recurse -Force } } } #Deleting the folders which have been emptied of files foreach ($emptyFolder in $currentChildren){ If ($emptyFolder.PSIsContainer -eq $true){ $updatedchildren = Get-ChildItem -Force -LiteralPath $emptyFolder.fullname -File if ($updatedchildren.count -eq 0){ write-host "Deleting folder $emptyFolder" -foreground "green" Remove-Item -exclude $filesToSave -path $emptyFolder.fullname -Recurse -Force } } } } } Foreach($path in $folderlocations){ DeleteFileAndFolders $path }