Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get the last file by Name

Posted on 2011-09-07
7
Medium Priority
?
225 Views
Last Modified: 2012-05-12
I have some back up files that are arranged in a directory by filename and date. for Instance, I have

BU_file_090120110800
BU_file_090220110800
BU_file_090320110800
BU_file_090420110800
BU_file_090420112100

I want to get the last file created by the filename which would be BU_file_090420112100 by reading he filename and not the file attributes. The attribute create date might work most of the time but the only 100% gauraunteed wa to handle is to use the file name to retreive. can someone help me on how to accomplish?

Thanks





0
Comment
Question by:earngreen
[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
7 Comments
 
LVL 4

Expert Comment

by:GeoffHarper
ID: 36498842
I haven't tested this vbscript...
Dim FSO, FileList(), FileListKey()
 
Sub AddFile(strFilename)
	Dim pos, buf, strFilenameKey

	buf = GetFileName(strFilename)

	strFilenameKey = Mid(GetFileName(buf,13,4) & Mid(GetFileName(buf,9,4) & Mid(GetFileName(buf,17,4)

	If (UBound(FileList) = 0) And (FileList(0) = "") Then
		FileList(0) = strFilename
		FileListKey(0) = strFilenameKey
	Else
		ReDim Preserve FileList(UBound(FileList)+1)
		ReDim Preserve FileListKey(UBound(FileListKey)+1)
		FileList(UBound(FileList)) = strFilename
		FileListKey(UBound(FileListKey)) = strFilenameKey
	End If

End Sub

Sub SortFilenames()
	Dim i, j, swap

	For i = 0 To (UBound(FileList)-1)
		For j = (i+1) To UBound(FileList)
			If FileListKey(i) < FileListKey(j) Then
				swap = FileList(i)
				FileList(i) = FileList(j)
				FileList(j) = swap
				swap = FileListKey(i)
				FileListKey(i) = FileListKey(j)
				FileListKey(j) = swap
			End If
		Next
	Next

End Sub

Set FSO = CreateObject("Scripting.FileSystemObject")

ReDim FileList(0)
FileList(0) = ""
ReDim FileListKey(0)
FileListKey(0) = ""

Set FO = FSO.GetFolder(Server.MapPath("./"))
For Each thisFile In FO.Files
	AddFile thisFile.Path
Next
Set FO = Nothing

SortFilenames

Response.Write("Last file: " & FileList(0))

Open in new window

0
 
LVL 9

Expert Comment

by:AriMc
ID: 36498870
First you need to use opendir, readdir and closedir to read the filenames into an array.

Next, since your filenames do not directly sort into chronological order, you should write a small function to rearrange the characters:

   BU_file_mmddyyyyhhmm   --->   BU_file_yyyymmddhhmm

(here mm represents the month, dd the day and yyyy the year)

Now, if you sort the array (with PHP's sort function), the latest file is in the last array slot
and you just need to rearrange the filename characters back to their original places:

   BU_file_yyyymmddhhmm   --->   BU_file_mmddyyyyhhmm




0
 

Author Comment

by:earngreen
ID: 36498928
Sorry, I need this in vb.net and unintentionall posted to PHP zone. I have contacted the moderator.
0
Independent Software Vendors: 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!

 
LVL 9

Expert Comment

by:AriMc
ID: 36498953
Ok, GeoffHarper's code seems to do just about the same procedure in VB, although I didn't check the exact character positions used in calls to the MID-function.

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 36499184
Try following UNTESTED code


Dim dTable As New DataTable
dTable.Columns.Add("fDate", GetType(DateTime))
dTable.Columns.Add("FileName", GetType(String))
Dim FileName, dateStamp as string
For each File in IO.Directory.GetFiles("path")
    FileName = IO.Path.GetFileNameWithoutExtension(File)
    dateStamp = FileName.Split("_")(2)
    Dim drow As DataRow = dTable.NewRow()
    drow(0) = DateTime.ParseExact(dateStamp, "MMddyyyyHHmm", CultureInfo.InvariantCulture)
    drow(1) = File
    dTable.Rows.Add(drow)
Next

dTable.DefaultView.Sort = "fDate Desc"
Dim LatestFile As String = dTable.DefaultView.Item(0).Item(1)
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36499450
try
Dim FileList = Directory.GetFiles("YourPath")
Dim FileDict As New SortedDictionary(Of Date, String)
For Each x In FileList
    Dim FileDate = x.Split("_").Last
    FileDict.Add(Date.ParseExact(FileDate, "MMddyyyyhhmmss", CultureInfo.CurrentCulture), x)
Next

Open in new window

with your dates sorted inthe keys, you have the earliest file in the value of the first ket etc...
Dim EarliestFile = FileDict.Item(0)

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
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…

609 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