Solved

files in a foler with date as part of file name

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

21 Experts available now in Live!

Get 1:1 Help Now