Combining two log files into one using PowerShell
04 February 2016
Here’s the more or less finished script from my previous post.
My IRC logs are structured into folders like this:
irclogs_old/
UnrealTournamentNet/
#game.log
#catpix.log
query.log
ChatNet/
nickserv.log
#erp_chat.log
------
irclogs_new/
UnrealTournamentNet/
#newgame.log
#catpix.log
friendguy.log
Horison/
#news.log
Because of this, the script needs to append new log files into old ones if they exist (#catpix.log in this example), create a new log file if an old one doesn’t exist, and also create new folders for networks that don’t have old logs but have new ones.
It works for my use case, but if the folder structure is any deeper than this the script needs to be modified. Replace 1 and 2 on the first two lines with the names of the folders your old and new logs are in, respectively.
$origFolder = join-path $pwd "1"
$newFolder = join-path $pwd "2"
Get-ChildItem -recurse -include "*.log" -path $newFolder |
ForEach-Object {
$folderPath = join-path $origFolder $_.DirectoryName.Split("\")[-1]
$newPath = join-path $folderPath $_.Name
if((Test-Path $folderPath) -eq 0) { mkdir $folderPath }
Add-Content -NoNewline -path $newPath -value (Get-Content $_ | Out-String)
}
It takes the logs from folder 2, and appends them after logs in folder 1. If a file doesn’t exist it should be automatically created by Add-Content, but the command can’t create folders so we need to check for that separately. It only goes one folder deep, so it needs to be modified if the folder structure is different.
You’ll probably want to backup your logs before using this just in case. It took a couple of minutes to go through my 700MiB of logs. Odds are it could be optimized to be faster but whatever.