In a recent article we covered how to remove old Server Logging and Report Results files.

Some of the feedback that we get around this subject is that there can be an uncertainty around whether all of the files should be deleted from both the database and the U4BW Application Server.

However, having several hundred thousand files in the U4BW Server Logging directory can be an issue when you try to look at any of the files, as File Explorer can take considerable time to show and sort the files.

As an alternative to using the in built U4BW options, it may be a case of leaving the files in the database, but removing them from the Application Server.  Rather than a global delete, it may be preferable to remove files that over a certain number of days old – 30 days for example.

Although it is possible to do this manually it is easier to do it with a Powershell command which can then be scheduled to run once a month.

Before running the command below you will need to edit three variables at the top of the script as tell the script which directory the files are located in and how many days you want to keep.

Please note, the following code is not supported and should be verified in a non-production environment before running in production.

# Edit these variables

#Location of the Server Logging Directory
$ServerLogging = "C:Program FilesUNIT4 Business World On! (v7)Data Filesabwt05Server Logging"

#Location of the Report Results Directory
$ReportResults = "C:Program FilesUNIT4 Business World On! (v7)Data Filesabwt05Report Results"

#How many days do you want to keep
$Daysback = "30"

# End of editable variables

$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays(-$Daysback)

$ServerLoggingBefore = (get-childitem $ServerLogging).Count
$ReportResultsBefore = (get-childitem $ReportResults).Count

$now = Get-Date

"Starting to delete Server Logging " + $now

Get-ChildItem $ServerLogging | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

"Allowing system to catch up for 1 minute..."
Start-Sleep 60

$now = Get-Date

"Starting to delete Report Results " + $now

Get-ChildItem $ReportResults | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

$ServerLoggingAfter = (get-childitem $ServerLogging).Count
$ReportResultsAfter = (get-childitem $ReportResults).Count

$ServerLoggingDeleted = $ServerLoggingBefore - $ServerLoggingAfter
$ReportResultsDeleted = $ReportResultsBefore - $ReportResultsAfter

"Files before delete = " + $ServerLoggingBefore
"Files after delete = " + $ServerLoggingAfter
"Number of files deleted = " + $ServerLoggingDeleted

"Files before delete = " + $ReportResultsBefore
"Files after delete = " + $ReportResultsAfter
"Number of files deleted = " + $ReportResultsDeleted