The following script deletes all files older than 20 days
'Create File System Object
Set filesys = CreateObject("Scripting.FileSystemObject")
'Specify Parent folder
Set Directory = filesys.GetFolder("C:\Program Files\Logs")
Set Files = Directory.Files
'Delete files
For Each Modified in Files
If DateDiff("D", Modified.DateLastModified, Now) > 20 Then Modified.Delete
Next
The following script deletes all subfolders including files in them that are older than 20 days
'Create File System Object
Set filesys = CreateObject("Scripting.FileSystemObject")
'Specify Parent folder
Set Directory = filesys.GetFolder("C:\Program Files\Logs")
Set Folders = Directory.Subfolders
'Delete folders and files
For Each Modified in Folders
If DateDiff("D", Modified.DateLastModified, Now) > 20 Then Modified.Delete
Next
The following deletes subfolders and files in the root folder older than 20 days
Set filesys = CreateObject("Scripting.FileSystemObject")
Set Directory = filesys.GetFolder("C:\Program Files\Logs")
Set Files = Directory.Files
For Each Modified in Files
If DateDiff("D", Modified.DateLastModified, Now) > 20 Then Modified.Delete
Set Folders = Directory.Subfolders
For Each Modified in Folders
If DateDiff("D", Modified.DateLastModified, Now) > 20 Then Modified.Delete
Next
Using the DateDiff you can specify "D" for days "W" for weeks and "M" for months. Change the number to represent the value you want to specify.
Specify the relevant edited script as a file to execute in a scheduled task and you are set.
3 comments:
THANK YOU!!! for this post and information!!! This is exactly what I was needing and looking for. I am very junior at scripting and this is exactly the help I needed. Thank you!
Glad I could help!
Guys,
This will not work for remote computers. Admins need to manage multiple computers. Below is the script that can be used to delete folders in multiple remote computers without having tio login to them.
Below script will delete folders older than 15 days. you can change the $days parameter though
D$\Program Files (x86)\Research In Motion\BlackBerry Enterprise Server\Logs is the UNC path for Blackberry Log folder. You can change the directory where your logs/folders are located.
list all your servername is servers.txt file and it should located in the same directory as this script.
#============Begin of the Script=======================
cd C:\Scripts\Powershellscripts\deletefiles ----> change it to the directory you wanna out this script to
$Days = "15"
$Now = Get-Date
$LastWrite = $Now.AddDays(-$days)
$server = get-content servers.txt
foreach ($node in $server)
{
get-childitem -recurse "\\$node\D$\Program Files (x86)\Research In Motion\BlackBerry Enterprise Server\Logs" | Where-Object {$_.LastWriteTime -le $LastWrite} | remove-item -recurse -force
}
#========End of the Script=================
save the script as .ps1 and run it... You can schedule it via batch file... That why you need to add Change Directory command at the beginning of the script..
Have fun..
Post a Comment