What is this?

This is basically where I write down stuff that I work with at my job as a GIS Technical Analyst (previously system administrator). I do it because it's practical for documentation purposes (although, I remove stuff that might be a security breach) and I hope it can be of use to someone out there. I frequently search the net for help myself, and this is my way of contributing.

Thursday, March 24, 2011

Keep your log folders tidy with movelogs.exe

Do you sometimes need to keep a log folder tidy? Well, I do. All the time. We have some webservers who create an awful lot of logs, and since I didn't really find a Windows replacement for Linux' logrotate I tried to write a batchfile to get rid of logfiles that were older than a certain number of days. Eventually I gave up and decided it was easier to write it in c#. I suppose powershell would have been even better, but who cares as long as the job gets done?
I suppose I'm not the only sysadmin out there who might need this from time to time, so here it is: movelogs.exe.

It will search a folder for files with a given extension, then apply another extension of your choice to these files providing they are older than a given number of days.

For instance the following command and parameters:

movelogs.exe c:\temp .log .oldlog 30
will search the folder c:\temp for all files ending with .log and attach .oldlog to the filename if the file is older than 30 days.

Personally I call movelogs from a small batchscript who will delete the .oldlog files after movelogs.exe is done. In other situations you might want to move them to a external dump-folder or something else.

Example myservice_deletelogs.bat:
movelogs.exe c:\temp\myservice .log .oldlog 30
del /q c:\temp\myservice\*.oldlog

I have provideed the executable, but if you want to compile it yourself - here's the c# source (should compile without problems in Visual Studio).


using System;
using System.Text;
using System.IO;

namespace movelogs
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 4)
            {
                string strFolder = args[0];
                string strExtension = args[1];
                string strNewExtension = args[2];
                System.DateTime datetimeNow = DateTime.Now;
                try
                {
                    int intFileRenameAge = Int16.Parse(args[3]);
                    string[] strFileList = Directory.GetFiles(@strFolder);
                    foreach (string strFileName in strFileList)
                    {
                        FileInfo fileinfoMyFile = new FileInfo(strFileName);
                        System.DateTime datetimeMyFileDate = fileinfoMyFile.LastWriteTime;
                        TimeSpan timespanFileAge = datetimeNow - datetimeMyFileDate;
                        if (strFileName.EndsWith(strExtension))
                        {
                            int intFileAge = timespanFileAge.Days;
                            if (intFileAge > intFileRenameAge)
                            {
                                Console.WriteLine("{0} is older than {1} days", strFileName, intFileRenameAge);
                                try
                                {
                                    File.Move(strFileName, strFileName + strNewExtension);
                                }
                                catch
                                {
                                    Console.WriteLine("Problem moving {0}. Possible reasons: file in use or permissions.", strFileName);
                                }
                            }
                        }
                    }
                }
                catch (DirectoryNotFoundException)
                {
                    Console.WriteLine("Problem opening folder: {0}", strFolder);
                }
                catch (FormatException)
                {
                    Console.WriteLine("Wrong format for number of days. Try a number");
                }
            }
            else
            {
                Console.WriteLine("movelogs.exe will scan a folder for files with a given extension who are older than a given number of days and then add a new exension them.");
                Console.WriteLine("v1.0
kjetil.pettersson@gmail.com\n");
                Console.WriteLine("Usage: ");
                Console.WriteLine("movelogs.exe [foldername] [extension] [new extension] [age in days]");
                Console.WriteLine("Example:");
                Console.WriteLine("movelogs.exe c:\\temp .log .oldlog 30");
            }
        }
    }
}
 

4 comments:

  1. This is very useful for me. I have some very large IIS logs.

    Regards,
    Gert from Holland!

    ReplyDelete
  2. Nice. Appreciate the feedback.

    ReplyDelete
  3. Perhaps you could add the possibility to supply a parameter to scan through sub folders?

    XenM

    ReplyDelete
  4. Much appreciated!

    Mario

    ReplyDelete