VB.net console application for Deleting old files from a particular folder

How are you guys? Happy New Year! We were quite busy and quiet during last few weeks. Here we are once again, coming up with a simple, yet powerful utility which could make a system administrator’s life bit easier.

We have a 12 years old Oracle database server with oracle data folder residing in D:\ drive with less than 20GB total free space where we do a full export everyday by late night.

Each full export (.dmp) file is almost 5GB, thus occupying the entire 20GB by every forth day. We were painfully following up the schedules and deleting the files to preserve much valued disk space.

Well, finally we decided to write a small console application to handle this task through a scheduled task. You can download the entire solution from following link

https://skydrive.live.com/redir?resid=68080371B15625F8!126&authkey=!AHiWHLBzE1x1YdU

The executable could be downloaded from following link

https://skydrive.live.com/redir?resid=68080371B15625F8!127&authkey=!AKmvC5fF61iSkiA

 

 

We developed this solution using Visual Studio Express for Desktop, ie, akka free version of VS 2012. You may need to download the same, so that you can open the solution.

However, to make stuff easier, we are copying the module1.vb code over here

[sourcecode language="vb" padlinenumbers="true" wraplines="true" gutter="false"]
Imports System.Console
Imports System.IO
Imports System
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Text
Imports System.Environment




Module Module1


    Sub Main()
        Dim strStartupArguments() As String, intCount As Integer
        Dim fldrName As String, fileType As String, nDays As Integer

        strStartupArguments = System.Environment.GetCommandLineArgs
        For intCount = 0 To UBound(strStartupArguments)
            ' Console.WriteLine(strStartupArguments(intCount).ToLower)
            ' Console.WriteLine(strStartupArguments(intCount))
            Select Case strStartupArguments(intCount).ToLower
                Case "-dfolder"
                    ' fldrName = strStartupArguments(intCount).ToLower
                    fldrName = strStartupArguments(intCount + 1)
                    'Console.WriteLine(fldrName)
                Case "-ftype"
                    fileType = strStartupArguments(intCount + 1)
                    'Console.WriteLine(fileType)
                Case "-ndays"
                    nDays = strStartupArguments(intCount + 1)
            End Select
        Next intCount

        If ((fldrName Is Nothing) Or (fileType Is Nothing) Or (nDays = 0)) Then
            Console.WriteLine("No or not all arguments given, USAGE DeleteOldFiles.exe -dFolder <folder name> -fType <*.extention> -nDays <N> ")
            Console.WriteLine("Example: DeleteOldFiles.exe -dFolder C:\myfolder -fType *.txt -nDays 100")
            Console.WriteLine("         C:\myfolder --is the target folder from files will be deleted")
            Console.WriteLine("         *.txt --tells the system what kind of files should be deleted ")
            Console.WriteLine("         100 --defines file age in number of days")
            End
        End If

        Dim fileName = Date.Now.ToString("ddMMyyyy") & ".log"
        Dim filePath = IO.Path.Combine(fldrName, fileName)
        Using sw As StreamWriter = New StreamWriter(filePath)
            sw.WriteLine("File Name" + ";" + "Creation Date" + ";" + "Deletion Time")
            Try
                ' For Each file As IO.FileInfo In New IO.DirectoryInfo("D:\Documents").GetFiles("*.pdf")
                For Each file As IO.FileInfo In New IO.DirectoryInfo(fldrName).GetFiles(fileType)
                    '     Console.WriteLine((Now - file.CreationTime))
                    If file.IsReadOnly = False Then
                        If (Now - file.CreationTime).Days >= nDays Then
                            sw.WriteLine(file.Name + ";" + file.CreationTime + ";" + Now)
                            file.Delete()
                        End If
                    End If


                Next
            Catch ex As Exception
                Console.WriteLine(ex.ToString)
            End Try
        End Using


    End Sub
End Module
[/sourcecode]

The default solution name is “DeleteOldFiles” hence when you open and build the solution, the .exe file name would be DeleteOldFiles.exe

Usage syntax

DeleteOldFiles.exe –dFolder <driverletter:\foldername> –fType *.extension –nDays <number of days>

eg: DeleteOldFiles.exe –dFolder C:\temp –fType *.tmp –nDays 3

Now you can schedule a job, and let this small piece of application doing the cleanup job for you

Yes, it does a logging for you :), so that you can always check which files were deleted from the folder (for eg: C\temp). The log file location will be the same folder from which the files were permanently deleted.

 

Tested on Windows Server 2003, Windows 8 64Bit, Windows 2008 Server

regards,

admin

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.