Solved

files in a foler with date as part of file name

Posted on 2011-03-18
5
268 Views
Last Modified: 2012-06-27
Hi, i am using vb.net
i have a folder with load files. there is a standard naming convention
xxxccyymmdd.csv for examlple
abc20110318.csv. I need to loop thru the files and work out which is the latest file in the folder. I cannot assume to take the files modified date and use that. I need to work out which i sthe lastest file based on the data passed as apart of the file name

thanks
0
Comment
Question by:CraigLazar
5 Comments
 
LVL 14

Expert Comment

by:GeorgeGergues
ID: 35167078
use the Folder and FileInfo Objects to get the file name attribute

Parse the String of the Filename without the extension ,
get the last 8 chars into a string

Use the Convert Utility to get the date.

then push all those in a list , sort the list , and then reprocess the list.




0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35167115
Try this:

Imports System.IO
Imports System.Text.RegularExpressions

...

Dim files() As String = Directory.GetFiles("C:\temp\", "*.csv")
Dim latestFile As String = (From f In files _
                            Where Regex.IsMatch(Path.GetFileName(f), "^[a-z]{3}[0-9]{8}\.csv$") _
                            Order By Path.GetFileName(f).Substring(3) Descending).FirstOrDefault

Open in new window


I hope this helps.
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35167154
BTW, the code I posted assumes the filename only contains lower case chars.  If you also have upper case chars, you need to change the Regex.IsMatch as follows:

Regex.IsMatch(Path.GetFileName(f), "^[a-z]{3}[0-9]{8}\.csv$", RegexOptions.IgnoreCase)


0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 250 total points
ID: 35167181
Hi,

You can do it like this, but you have to notice that since you are sorting by the Date only, if there is more than 1 file for a day, it could return anyone of them, so if you have 2 files like this

abc20110318.csv
bbb20110318.csv

It will return 20110318 as the latest file, but wil not give you wich one of the two is, becaus both have the same date



Dim vecFiles() As String
        Dim intFile As Integer
        Dim strDatePart As String
        Dim alDates As New ArrayList

        'Get the files names
        vecFiles = Directory.GetFiles("C:\ISB\Folder")
        For intFile = 0 To vecFiles.Length - 1
            'Get the DatePart
            strDatePart = vecFiles(intFile).Substring(vecFiles(intFile).LastIndexOf("\") + 4, 8)
            alDates.Add(strDatePart)
        Next

        'Sort the files by the datepart
        alDates.Sort()

        'Show the latest
        MsgBox("Latest File: " & alDates(alDates.Count - 1))

Open in new window

0
 
LVL 4

Author Closing Comment

by:CraigLazar
ID: 35183947
thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question