Solved

Get the last file by Name

Posted on 2011-09-07
7
217 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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 …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

23 Experts available now in Live!

Get 1:1 Help Now