Solved

Get the last file by Name

Posted on 2011-09-07
7
216 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

16 Experts available now in Live!

Get 1:1 Help Now