When I started working with BizTalk, the norm was to use bat files or VB Script. These can still be seen in many places since they a) work just fine, and b) the developers/administrators hasn't learned a new way to script these kind of things, mostly because a) they work just fine.
I myself as mentioned earlier prefer to use PowerShell nowadays.
In order to recursivly delete old files and folders, one line of PowerShell script is all that is needed:
get-childitem -path 'c:\temp\test' -recurse |
where -FilterScript {$_.LastWriteTime -le [System.DateTime]::Now.AddDays(-30)}
| Remove-Item -recurse -force
This script simply traverses the specified folder (why not change it to take the folder and date as a parameter) and does so recursivly. All files older than 30 days in this case is then deleted. By using the -force parameter, locked and hidden files will not cause an error.
In order to have this run automatically on a server, it needs some adjustments. Add a Scheduled Task and set the Program/script to run to Powershell and then add the arguments as so:
-noninteractive -command "& {get-childitem -path 'c:\temp\test' -recurse | where -FilterScript {$_.LastWriteTime -le [System.DateTime]::Now.AddDays(-30)} | Remove-Item -recurse -force}"
Set the security options for the task to "Run whether user is logged on or not" and also check the "Run with highest privileges" checkbox.
No comments:
Post a Comment