[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

files in a foler with date as part of file name

Posted on 2011-03-18
5
Medium Priority
?
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

650 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