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

后端 未结 3 1368
隐瞒了意图╮
隐瞒了意图╮ 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

    0 讨论(0)
  • 2021-01-15 12:52

    If you use WMI, it supports wildcards.

    Dim strPath
    
    strFile = "*.*"
    If WScript.Arguments.Count > 1 Then
        strPath = WScript.Arguments.Item(0)
        strFile = WScript.Arguments.Item(1)
    Elseif WScript.Arguments.Count = 1 Then
        strPath = WScript.Arguments.Item(0)
    Else
    
    End If
    
    Set objFso = CreateObject("Scripting.FileSystemObject")
    If Not objFso.FolderExists(strPath) Then
        WScript.Echo "Folder path does not exist."
        WScript.Quit
    Else
        'Remove any trailing slash
        If Right(strPath, 1) = "\" Then
            strPath = Left(strPath, Len(strPath) - 1)
        End If
    End If
    Set objFso = Nothing
    
    If Not IsNull(strPath) And strPath <> "" Then
        strQuery = strPath & "\" & strFile
    Else
        strQuery = strFile
    End If
    
    strQuery = Replace(strQuery, "*", "%")
    strQuery = Replace(strQuery, "?", "_")
    
    strQuery = Replace(strQuery, "\", "\\")
    
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2")
    
    Set colFiles = objWMIService.ExecQuery _
        ("Select * From CIM_DataFile Where FileName Like '" & strQuery & "'")
    
    For Each objFile in colFiles
        WScript.Echo "Access mask: " & objFile.AccessMask
        WScript.Echo "Archive: " & objFile.Archive
        WScript.Echo "Compressed: " & objFile.Compressed
        WScript.Echo "Compression method: " & objFile.CompressionMethod
        WScript.Echo "Creation date: " & objFile.CreationDate
        WScript.Echo "Computer system name: " & objFile.CSName
        WScript.Echo "Drive: " & objFile.Drive
        WScript.Echo "8.3 file name: " & objFile.EightDotThreeFileName
        WScript.Echo "Encrypted: " & objFile.Encrypted
        WScript.Echo "Encryption method: " & objFile.EncryptionMethod
        WScript.Echo "Extension: " & objFile.Extension
        WScript.Echo "File name: " & objFile.FileName
        WScript.Echo "File size: " & objFile.FileSize
        WScript.Echo "File type: " & objFile.FileType
        WScript.Echo "File system name: " & objFile.FSName
        WScript.Echo "Hidden: " & objFile.Hidden
        WScript.Echo "Last accessed: " & objFile.LastAccessed
        WScript.Echo "Last modified: " & objFile.LastModified
        WScript.Echo "Manufacturer: " & objFile.Manufacturer
        WScript.Echo "Name: " & objFile.Name
        WScript.Echo "Path: " & objFile.Path
        WScript.Echo "Readable: " & objFile.Readable
        WScript.Echo "System: " & objFile.System
        WScript.Echo "Version: " & objFile.Version
        WScript.Echo "Writeable: " & objFile.Writeable
    Next
    

    EDIT..........

    You can use a WMI event script with the __InstanceCreationEvent to monitor for new file creation in a specific folder. It looks like this:

    strSource = "C:\\somefilepath\\withdoubleshlashes"
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2")
    
    Set colEvents = objWMIService.ExecNotificationQuery _
        ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
            & "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _
            & "TargetInstance.GroupComponent= " _
            & "'Win32_Directory.Name=""" & strSource & """'")
    
    Do While True
        Set objEvent = colEvents.NextEvent()
        copyFile(objEvent.TargetInstance.PartComponent)
    Loop
    

    For a full explanation, you can read Monitoring and Archiving Newly Created Files on my blog.

    0 讨论(0)
  • 2021-01-15 13:17

    For your example, the easiest way to do this is to use the inStr (In String)function. I find it works in 99% of my wild card tasks. So, in your example, instead of using:

    If file.Name = fileName Then
    

    use:

    If inStr(file.Name, filename) Then
    

    This doesn't actually allow for wildcards(*) as it won't find a match(with the asterisk in the argument), so you would need to strip the wildcard from the string and replace it with nothing (or just train the user to not use wildcards):

    Replace(filename,"*", "")
    

    However, the inStr function does allow for partial or full matches which makes it suitable for most wildcard tasks. Therefore, if your file name is pic.jpg, whether the user searches for:

    pic or jpg or p or c or pi etc.

    It will return a match. Keep in mind though, that the instr function returns a number where the match shows up in the string. So, if it doesn't create a match, the result will be 0. I've run into examples where NOT doesn't work or I've needed to use the full syntax which in this case would be:

    If inStr(file.Name, filename)<>0 Then
    
    0 讨论(0)
提交回复
热议问题