search specific file in folder and get the lastmodifid

drtopserv
drtopserv used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

Top Expert 2013

Commented:
@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

Author

Commented:
Also, I`D appretiate having the change very close to my code with addition/fixing places where i have to use "LIKE" operator
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

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
IT / Software Engineering Consultant
Top Expert 2016
Commented:
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

Author

Commented:
thnx

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial