Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / C#

Log File Cleanup App for Windows

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
20 Mar 2009CPOL 27.5K   328   14   3
An app written in C# .NET to clean up old log files from Windows directories

Introduction

This is a small application written in C# .NET that can delete files from any Windows directory. It is a command line tool that can be run via a batch file (included in the Debug directory of the project).

Background

The basic idea of this tool is to clean up old files that keep on lingering on in the system especially log files. The tool accepts *.txt/*.log/*.zip inputs and deletes them from the specified folder. The age of the files can also be supplied as a parameter to the tool. 

Using the Code

The tool can be used by writing the following lines in a batch file:

rem the format for the arguments to be passed to the logCleanup.exe is as follows
rem 1. path of the folder (enclosed within double quotes / replace all '/' with '//')
rem 2. number of days e.g: 10
rem 3. extension of files to be deleted. limited only to txt / log / zip
rem 4. mode. limited to top / all. top-deletes files in that directory only. 
				all-deletes files in all subdirectories too.
logCleanup.exe "C:\\temp" 30 txt top
rem pause  

The logCleanup.exe file is generated by compiling the following code segment:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace logCleanup
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] sFiles = { };
            string sFileName = "";
            int iCount = 0;
            if ("txt" != args[2].ToString() && "log" != args[2].ToString() && 
						"zip" != args[2].ToString())
            {
                Console.WriteLine("Only zip, txt and log files can be deleted");
                Console.Read();
                return;
            }
            if (Directory.Exists(args[0].ToString()))
                if ("top" == args[3].ToString())
                    sFiles = Directory.GetFiles(args[0].ToString(), 
			"*."+args[2].ToString(), SearchOption.TopDirectoryOnly);
                else if ("all" == args[3].ToString())
                    sFiles = Directory.GetFiles(args[0].ToString(), 
			"*." + args[2].ToString(), SearchOption.AllDirectories);

            Console.WriteLine("Total Files Parsed: " + sFiles.Length);
            for (iCount = 0; iCount < sFiles.Length; iCount++)
            {
                sFileName = sFiles.GetValue(iCount).ToString();
                if (1 == DateTime.Compare(DateTime.Now, 
		File.GetCreationTime(sFileName).AddDays(Convert.ToDouble(args[1]))))
                {
                    Console.WriteLine("File Deleted: " + sFileName);
                    File.Delete(sFileName);
                }
            }
        }
    }
}	

Points of Interest

This code currently only works with the Date of Creation of a file. It can easily be modified to use the Last Updated time, etc.

History

  • Initial version 0.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionon a positive note Pin
donjev23-Feb-13 7:09
donjev23-Feb-13 7:09 
I found this interesting as a basis for an application I am coding for work that looks over all of our store PC's and deketes log files from a number applications that don't clean up after thenselves. Installing CCleaner on all of our retailer store PC's is not really an option for us. I'll post the code when it is completed and certainly hope to get some favourable, or at least constructive, feedback for my efforts on here.
General[My vote of 2] Coding oddities Pin
John Brett24-Mar-09 6:05
John Brett24-Mar-09 6:05 
GeneralCCleaner Pin
thund3rstruck20-Mar-09 10:17
thund3rstruck20-Mar-09 10:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.