Jumlins TechBlog

The coding hell and the daily IT of Niklas Jumlin

May 24th, 2012

At work I was tasked with writing a script that would clean up and archive logs from an application that didn’t have any decent log rotation built-in. The application would however rotate the logs when the application’s service was restarted and then rename the old log with an incremental number suffix. So we added a scheduled task that would restart the service every night in order to get the logs rotated and free from being locked.

So what this script will do is to take any log which contains a number at the end and archive it to another location, it will then replace the archives last modified date to the original last modified date of the actual log file. After that it will simply delete archives older than X-days based on its last modified date and time.

You can of course configure the script to look for logs named in a different way. This is quite simple:

## Define files to match
$LogFileMatch = "*scplog?"
## * = Any or none characters
## ? = Any character (Must be one)

The script will log events to screen and also to a log file.
It will also log every event to Windows built-in EventLog if executed with administrator privileges.
The script will also handle all exit codes from 7-zip command-line which is the program it’s utilizing in order to archive files.

Configuration settings that can and should be edited in the script are commented at the top of the script.

Read the rest of this entry »

October 26th, 2010

A colleague asked me if I could write a script that would move files within a folder or its subfolders that were older than one year into a new location but still keep the directory structure.
I figured it would be quite a challenge so I decided to do it for him; kind as I am =)

As I haven’t been providing this blog with something new in a long time I figured I could make it useful to more people if I added a few things, like different date formats and other various settings.

So here it goes:
Read the rest of this entry »