VBscript在特定时间范围内检查是否存在文件(能够使用通配符)

时间:2012-10-24 15:53:57

标签: windows vbscript

早上好,    我一直试图将一个VBscript整合在一起,该脚本在脚本被取消时从用户那里获取文件路径和文件名(可能有一个通配符)。然后,脚本将检查指定目录中是否有与提供的文件名匹配的文件,然后查看上次修改日期以查看它是否在特定时间范围内(即早上6点加减5分钟)创建/修改。然后它会将所述文件复制到一个zip文件中。

到目前为止,我已经能够使参数正常工作了,我设置了它来获取当前时间,查看文件夹中的文件并将硬编码文件名与文件夹中的文件名匹配。这就是我到目前为止所做的。

currentTime = Now()

filePath = Wscript.Arguments.Item(0)
fileName = Wscript.Arguments.Item(1)

Set fileSystem = CreateObject("Scripting.FileSystemObject")
Set directory = fileSystem.GetFolder(filePath)

For each file in directory.Files
    If file.Name = fileName Then
        Wscript.echo file.Name & " " & file.DateLastModified
    end if
Next

我是VBscript noob,我期待着学习的方式!

CAP3

3 个答案:

答案 0 :(得分:6)

如果您使用WMI,则它支持通配符。

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 ..........

您可以使用带有__InstanceCreationEvent的WMI事件脚本来监视特定文件夹中的新文件创建。它看起来像这样:

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

要获得完整的解释,您可以在我的博客上阅读Monitoring and Archiving Newly Created Files

答案 1 :(得分:1)

此答案使用正则表达式。为了使其工作,它将您的模式格式重写为正则表达式格式。例如*.txt将成为^.*[.]txt$

以下列出了C:\Temp上次在上午5:55到上午6:05之间修改的文本文件:

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

参考文献:

答案 2 :(得分:0)

对于您的示例,最简单的方法是使用inStr(In String)函数。我发现它在99%的外卡任务中都有效。因此,在您的示例中,而不是使用:

If file.Name = fileName Then

使用:

If inStr(file.Name, filename) Then

这实际上不允许使用通配符(*),因为它找不到匹配项(参数中带有星号),因此您需要从字符串中删除通配符并将其替换为没有(或只是训练用户不使用通配符):

Replace(filename,"*", "")

但是,inStr函数允许部分或完全匹配,这使其适用于大多数通配符任务。因此,如果您的文件名是pic.jpg,则用户是否搜索:

pic或jpg或p或c或pi等。

它会返回一个匹配项。但请记住,instr函数返回一个数字,其中匹配显示在字符串中。因此,如果它没有创建匹配,则结果将为0.我已经遇到NOT无效或我需要使用完整的示例语法在这种情况下将是:

If inStr(file.Name, filename)<>0 Then