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

https://intersect.co.uk/agresso-business-world/agresso-clean-up-of-blobs/

And we also did an article on deleting the files from the U4BW Application Server when they are over a particular number of days old.

https://intersect.co.uk/agresso-business-world/agresso-delete-blobs/

This article also shows how to use Powershell to clean up files over a certain number of days old, but instead of deleting the files it compresses them to ZIP files.  The log files which were created in January 2018 will be compressed to a single ZIP file called Logs-2018-1. February 2018 will go to Logs-2018-2 and so on.  Report Result files will be on the same basis but rather than a Logs prefix will have a prefix of Report – Report-2018-01.

If you are thinking of archiving all of the files from the U4BW Application server before you purge them from the database, then you may find that you will have lost some older files.  The reason behind this is that U4BW software does not get upgraded.  So if you upgrade from Milestone 5 to Milestone 7 for example, then the Milestone 7 installation will be a new install and only contain Server Logging and Report Results files from Milestone 7.  A lot of organisations may have upgraded several times and each time used a new U4BW Application Server, so the only complete set of files would be in the database.

The script uses WinRar to compress the files.  It is possible to use other compression packages but you will have to change the executable name and syntax for the two lines that do the compression.

WinRar does not have to be installed on the U4BW Application Server, you can simply copy the WINRAR.EXE file from another installation and put it into a directory on the server.  If you do this, check that the $rar variable is correct in the script.

Once the files have been archived WinRar deletes the original files and moves them in to the Recycle Bin.

Compressing files is CPU intensive.  This isn’t an issue, but it is better to carry out this routine outside of normal business hours as it could effect performance.

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

# Edit these variables
#===================================================

	# Get the files which should be moved, without folders
	$ServerLogging = 'C:Program FilesUNIT4 Business World On! (v7)Data Filesabwt05Server Logging' 
	$ReportResults = 'C:Program FilesUNIT4 Business World On! (v7)Data Filesabwt05Report Results'
	
	# Target Filder where files should be moved to. 
	$targetPath = 'C:Archive'

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

	#Location of rar.exe program
	$rar = 'C:Program FilesWinRARrar.exe'

#===================================================	
# End of editable variables

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

	# Process Server Logging
	$dir = Get-ChildItem $ServerLogging | Where-Object {$_.LastWriteTime -lt $DatetoArchive} 

	foreach ($file in $dir)
	{$year = $file.LastWriteTime.Year.ToString()
	$month = $file.LastWriteTime.Month.ToString()
	$file = $file.FullName

	# Set Directory Path
	$Directory = $targetPath 
	if (!(Test-Path $Directory))
	{
	New-Item $directory -type directory
	}

	#Set Archive Name
	$Archive = $Directory +"" +"Log-" +$year +"-" +$month

	# Add file to zip archive
	&$rar a -dr $archive $file 
	}

	# Allow system to catch up for 1 minute
	start-sleep 60

	#Process Report Results
	$dir = Get-ChildItem $ReportResults | Where-Object {$_.LastWriteTime -lt $DatetoArchive} 

	foreach ($file in $dir)
	{$year = $file.LastWriteTime.Year.ToString()
	$month = $file.LastWriteTime.Month.ToString()
	$file = $file.FullName

	# Set Directory Path
	$Directory = $targetPath 
	if (!(Test-Path $Directory))
	{
	New-Item $directory -type directory
	}

	#Set Archive Name
	$Archive = $Directory +"" +'Report-' +$year +"-" +$month

	# Add file to zip archive
	&$rar a -dr $archive $file 
	}