Solved

files in a foler with date as part of file name

Posted on 2011-03-18
5
263 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now