Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

 

       ' 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.

  • Leaders
Posted

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"

 

   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 don't 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}"

[sIGPIC]e[/sIGPIC]
Posted

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.....

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...