Rotate Logs



Script scans folder and finds all .LOG files. It then moves them to a subfolder based on filenames and adds the date/time to the filename. Someday this needs to be revisited to fix files with spaces in the name.


$LogPath="c:\SyncLogs\"
$database = Get-ChildItem $LogPath -Filter *.log

foreach ($filename in $database)
{
$filename = $filename -replace "....$"
$currentdate = get-date -format "yyyy-MM-dd-HHmmss"
$FileBackupLocation = $LogPath+"Backup\"+$Filename+"\"

if ( -Not (test-path $FileBackupLocation))
{
New-Item -Path $FileBackupLocation -ItemType Directory
}
Move-Item -path $LogPath$Filename".log" -destination $FileBackupLocation$Filename"-"$currentdate".log"
}