search specific file in folder and get the lastmodifid

Hi,
I would to edit the code to be able to search a specific  file like : Filename*.xlsx  (notice a file starting with static name then *)

the code below do the job but i have to give him a full name without using * , (what I need is to have * in the file)

sub filelastmodified(strFullFileName as string)

 Dim fs As Object, f As Object, s As String
     
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(strFullFileName)
     
    s = UCase(strFullFileName) & vbCrLf
    s = s & "Last Modified: " & f.DateLastModified
    'FileLastModified = s
    FileLastModified = f.DateLastModified
     
    Set fs = Nothing: Set f = Nothing

end sub
drtopservAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
With the FSO you have to loop through all the files. This obviously gets slow as there are more and more files in the folder.
Dim filter as String, FileLastModified as Date, Found as Boolean, LastFileName as String 

Filter = "Filename*.xlsx"
FileLastModified ="01-01-1900"
Found = false

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Files"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
For Each objFile in colFiles
    If objFile.Name Like filter Then
        Found = true
        If FileLastModified < objFile.DateLastModified Then
              FileLastModified = objFile.DateLastModified
              LastFileName = objFile.Name
        End If
    End If
Next

If Found Then
     Echo FileLastModified ':) 
     Echo LastFileName 'name of  the file 
End If

Open in new window

0
chaauCommented:
@MlandaT: VBScript does not have the LIKE operator. I do not want to steal your solution. Please change your answer to use a different pattern matching logic
0
drtopservAuthor Commented:
Also, I`D appretiate having the change very close to my code with addition/fixing places where i have to use "LIKE" operator
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

MlandaTCommented:
Guys... I think I have provided adequate guidance. Thanks chaau. Am driving out. Won't be back for a while... So please go for it.

One could pay around with instr and the extension I suppose
0
Bill PrewCommented:
Unfortunately, the filesystem object doesn't have any support for pattern matching when you are looking for files.  Typically there are two approaches to work around this.  

One is to iterate through all the files in the desired folders and then check the filename and extension to the desired patterns.  Functions like INSTR(), LEFT() and RIGHT() can be helpful there.  If you really do want to do true pattern matching then you can check each filename using a REGEX pattern and filter the matching files that way.  Let me know if you want an example of that.

A second approach can involve the use of the WMI DATAFILE collection, which does support pattern matching in the selection of files.  But the syntax of the WHERE clause is a little interesting.  But for simple pattern matching like you are doing could work.  Keep in mind though that it will return a collection of DATAFILE items, which have different properties than the FSO file object, and specifically dates can be harder to work with.  Here's a simple example of how that could be used (done in VBscript rather than VBA for simplicity of testing, but the same code works in VBA just adding the DIM and variable types).

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * From CIM_DataFile where Drive = 'C:' and Path = '\\temp\\' and Name LIKE '%copy%' and Extension = 'txt'")

For Each objItem in colItems
	WScript.Echo "LastModified(collection):" & objItem.LastModified
	WScript.Echo "Name(collection):" & objItem.Name
   FileLastModified objItem.Name
Next


Sub FileLastModified(strFullFileName)

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(strFullFileName)
     
    wscript.echo "Name(function):" & UCase(strFullFileName)
    wscript.echo "Last Modified(function):" & f.DateLastModified

    ' FileLastModified = f.DateLastModified
     
    Set fs = Nothing
    Set f = Nothing

End Sub

Open in new window

References:

WMI

https://msdn.microsoft.com/en-us/library/aa387236%28v=vs.85%29.aspx

REGEX

http://blogs.technet.com/b/heyscriptingguy/archive/2008/02/28/how-can-i-move-files-based-on-a-portion-of-the-file-name.aspx

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
drtopservAuthor Commented:
thnx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.