Trying to use VBScript to do this. I have a folder that has multiple files that include the server name with an extention of .vib. All the files show up as
servername.vm-<randomnumber><date>T<time>.vib
for example:
SERVERNAME.vm-255622016-08-31T040037.vib
What I need to do is grab all the files that contain servername*.vib, then find the last modified file of that list, then pull the date from that file. The date is in a xxxx-xx-xx format and always proceeds a T in the file name and seems to be in the same relative place.
So far, I've been able to get pretty close by using this other question.
However I'm having trouble with two things, finding files that only contain the servername and getting the date from the file itself.
Here's my code so far:
Set fso = CreateObject("Scripting.FileSystemObject")
Set list = CreateObject("ADOR.Recordset")
list.Fields.Append "name", 200, 255
list.Fields.Append "date", 7
list.Open
For Each f In fso.GetFolder("J:\Files").Files
If (fso.GetExtensionName(f) = "vib") And (fso.Contains(ServerName)) Then
list.AddNew
list("name").Value = f.Path
list("date").Value = f.DateLastModified
list.Update
End If
Next
list.Sort = "date DESC"
list.MoveFirst
WScript.Echo list("name").Value
list.Close
If I remove the fso.Contains(ServerName)
from the If
statement then the code runs and I get the latest vib file. I just can't figure out how to test for the servername being contained in the file so I only get files that have the server name in them.
I'm also stuck on how to pull the date from the file itself and can't begin to figure out where to start.
FileSystemObject
objects don't have a Contains
method. You can test the filename for the server name like this:
LCase(Left(f.Name, Len(ServerName))) = LCase(ServerName)
However, since you also want to extract the date, a regular expression might be a better approach:
Set re = New RegExp
re.Pattern = ServerName + "\.vm-\d+?(\d{4}-\d{2}-\d{2})T\d{6}\.vib"
With that you can test the presence of the server name like this:
re.Test(f.Name)
and extract the date like this:
For Each m In re.Execute(f.Name)
datestring = m.SubMatches(0)
Next