VBscript to check for the existance of a file (with the ability to use a wildcard) within a certain time frame

后端 未结 3 1371
隐瞒了意图╮
隐瞒了意图╮ 2021-01-15 12:34

Good morning all, I have been trying to pull together a VBscript that takes a file path and a file name (that may have a wildcard in it) from the user when the script is

3条回答
  •  星月不相逢
    2021-01-15 12:51

    This answer uses Regular Expressions. To make it work it rewrites your pattern format into regular expression format. e.g. *.txt will become ^.*[.]txt$.

    The following lists text files in C:\Temp last modified between 5:55 AM and 6:05 AM:

    strPath = "C:\Temp"
    strFile = "*.txt"
    startTime = 555
    endTime = 605
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(strPath)
    Set files = folder.Files
    
    Set re = New RegExp
    re.IgnoreCase = true
    re.Pattern = "^" + Replace(Replace(strFile, ".", "[.]"), "*", ".*") + "$"
    
    For Each f in Files
      Set matches = re.Execute(f.Name)
      If matches.Count > 0 Then
        HM = Hour(f.DateLastAccessed) * 100 + Minute(f.DateLastAccessed)
        If HM >= startTime And HM <= endTime Then
          WScript.Echo f.Name, f.DateLastAccessed
        End If
      End If
    Next
    

    References:

    • Regular Expression (RegExp) Object

    • Regular Expressions and Operators

    • Microsoft Beefs Up VBScript with Regular Expressions

    • Hey, Scripting Guy! Raising Eyebrows on Regular Expressions

提交回复
热议问题