Question Console application switch values

rajthampi

Member
Joined
Jul 16, 2008
Messages
18
Location
Kuwait
Programming Experience
Beginner
Hi guys

I am trying to make a simple console application for deleting files which are N days old and the console application should take three switches like following:

deleteoldfiles -dFolder C:\myfolder -fType *.dmp -nDays 3

The below module does it, however I am not sure whether the logic used to capture the switch values is appropriate or standard. Please help

VB.NET:
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

        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 (Now - file.CreationTime).Days > nDays Then file.Delete()
            Next
        Catch ex As Exception
            Console.WriteLine(ex.ToString)
        End Try

    End Sub

End Module
 
That's not bad but there are a few issues there, primarily lack of validation. Assuming that all three switches are required, here's how I would do it:
Sub Main(args As String())
    Dim dFolderIndex = Array.FindIndex(args, Function(s) s.Equals("-dFolder", StringComparison.InvariantCultureIgnoreCase))
    Dim fTypeIndex = Array.FindIndex(args, Function(s) s.Equals("-fType", StringComparison.InvariantCultureIgnoreCase))
    Dim nDaysIndex = Array.FindIndex(args, Function(s) s.Equals("-nDays", StringComparison.InvariantCultureIgnoreCase))

    If dFolderIndex = -1 OrElse fTypeIndex = -1 OrElse nDaysIndex = -1 Then
        'One or more commandline arguments not found.
        Return
    End If

    Dim upperBound = args.GetUpperBound(0)

    If dFolderIndex = upperBound OrElse fTypeIndex = upperBound OrElse nDaysIndex = upperBound OrElse
       dFolderIndex = fTypeIndex + 1 OrElse dFolderIndex = nDaysIndex + 1 OrElse
       fTypeIndex = dFolderIndex + 1 OrElse fTypeIndex = nDaysIndex + 1 OrElse
       nDaysIndex = dFolderIndex + 1 OrElse nDaysIndex = fTypeIndex + 1 Then
        'No value provided for one or more switches.
        Return
    End If

    Dim dFolder = args(dFolderIndex + 1)
    Dim fType = args(fTypeIndex + 1)
    Dim nDaysText = args(nDaysIndex + 1)
    Dim nDays As Integer

    If Not Directory.Exists(dFolder) OrElse
       Not Integer.TryParse(nDaysText, nDays) OrElse
       nDays < 0 Then
        'Invalid values for one or more switches.
        Return
    End If

    Dim currentTime = Date.Now

    Try
        For Each filePath As String In Directory.GetFiles(dFolder, fType)
            If currentTime.Subtract(File.GetCreationTime(filePath)).TotalDays >= 3.0 Then
                File.Delete(filePath)
            End If
        Next
    Catch ex As Exception
        Console.WriteLine(ex.ToString())
    End Try
End Sub
 
Last edited:
Thank you jm
Actually, I must say, I am surprised to see that you had put so much efforts to restructure the entire codes. Thank you very much. From the beginner's eyes, your coding looks like a feast :)

I will check it and update you.

regards,

raj
 
I realised that there was an issue with array indexing in my code because I moved some around and forgot to adjust Length to GetUpperBound. I have now fixed the issue and updated the code.
 
Thank you jm, a final favor

This application is scheduled to run once in 3 days, hence I would like to create a log file like 12012013.log or 15012013.log depending upon the date and write the details of files (file name, creation date etc) deleted from the folder. Would you please help?

regards,
 
Create a StringBuilder and call its Append and/or AppendLine method to add the desired text to it. When you're done, you can write that text out to a file like so:
Dim fileName = Date.Now.ToString("yyyyMMdd") & ".log"
Dim filePath = IO.Path.Combine(folderPath, fileName)

IO.File.WriteAllText(filePath, myStringBuilder.ToString())
 

Latest posts

Back
Top