?
Solved

files in a foler with date as part of file name

Posted on 2011-03-18
5
Medium Priority
?
276 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

765 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