?
Solved

Get the last file by Name

Posted on 2011-09-07
7
Medium Priority
?
223 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

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