Solved

Get the last file by Name

Posted on 2011-09-07
7
218 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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