Regex Problems

Jay1b

Well-known member
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
I have a program that creates logfiles, by using the applications name, the date and time and then .log at the end for the logfiles filename.

I want to retrieve a list of these logfiles. So far this successfully retrieves a list of ALL files in the directory, i need to match the name against the applications name and ".log" at the end of the filename. From my research i came up with something like this below, but it doesnt work.

Could someone guide me in the right direction please?

Code:
         Create a reference to the current running directory.
        Dim DirBase As New DirectoryInfo(StartupPath)
         Create an array representing the files in the current directory.
        Dim fi As FileInfo() = DirBase.GetFiles("*.*")
         Print out the names of the files in the current directory.
        Dim fiTemp As FileInfo
         Create array holding names of all sub-directories
        Dim di As DirectoryInfo() = DirBase.GetDirectories("*.*")

        For Each fiTemp In fi
            MsgBox(fiTemp.Name)

            Dim r As New Regex(^(GetExecutingAssembly.GetName.Name) & ".log"$)
             Find a single match in the input string.
            Dim m As Match = r.Match(fiTemp.Name)
            If m.Success Then
                .....................
            End If

        Next

Thanks.
 
Why use regular expressions? These simple string operations can be done with other functions and classes. The DirectoryInfo.GetFiles() method allows you to specify a filter which which is usually used to find a specific file type, but in your case you could let it filter out any files except those that start with your app name and end with ".log"

Code:
    Dim BaseDirectory As New DirectoryInfo(Application.StartupPath)
    Dim FileList As FileInfo() = BaseDirectory.GetFiles(GetExecutingAssembly.GetName.Name & "*.log") 
    ^ Will find files that start with the assembly name and end with .log
    
    Create array holding names of all sub-directories
    Dim SubDirs As DirectoryInfo() = DirBase.GetDirectories() 
    ^ I dont think you want the *.* filter here. Most likeley NONE of your subfolders will match this pattern. 
      *.* is more suitable for filenames than path names.

Two easy lines of code. If you need to extract the dates and times after this point, then use regex to find and extract matches to date/time string formats.

Depending on the date and time formats you are using these may or may not work.
Dates: "\d{1,2}/\d{1,2}/\d{2,4}"
Times: "\d{1,2}:\d{2}:\d{2}"
 
lol.....Thanks.....I can hardly believe i didnt notice the DirBase.GetFiles("*.*") bit! I think i will have to read my previous code, before i just cut and paste it :D

Thanks again.....
 
Back
Top