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

Windows VBscript,用于在特定时间范围内检查文件是否存在(能够使用通配符),windows,vbscript,Windows,Vbscript,大家早上好, 我一直在尝试组合一个VBscript,当脚本退出时,它从用户那里获取一个文件路径和一个文件名(其中可能有一个通配符)。然后,脚本将检查指定目录中与提供的文件名匹配的文件,然后查看上次修改的日期,查看是否在特定时间范围内(即上午6点加上或减去5分钟)创建/修改了该文件。然后它会将所述文件复制到zip文件中 到目前为止,我已经能够让参数工作,并且我已经设置好抓取当前时间,查看文件夹中的文件,并将硬编码文件名与文件夹中的文件名进行匹配。这就是我到目前为止所做的 currentTime =

大家早上好, 我一直在尝试组合一个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
我是一个vbscriptnoob,我期待着学习的方式


Cap3如果使用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

要获得完整的解释,您可以在我的博客上阅读。

对于您的示例,最简单的方法是使用inStr(字符串)函数。我发现它在99%的通配符任务中都有效。因此,在您的示例中,不要使用:

If file.Name = fileName Then
使用:

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

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

pic或jpg或p或c或pi等

它将返回一个匹配。但是请记住,instr函数返回一个匹配项出现在字符串中的数字。因此,如果不创建匹配,结果将为0。我遇到过一些例子,其中
NOT
不起作用,或者我需要使用完整的语法,在本例中是:

If inStr(file.Name, filename)<>0 Then
如果inStr(file.Name,filename)为0,则

此答案使用正则表达式。为了使它工作,它将您的模式格式重写为正则表达式格式。e、 g.
*.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
strPath=“C:\Temp”
strFile=“*.txt”
开始时间=555
结束时间=605
设置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),“.”,“[.]”,“*”,“*”+“$”
对于文件中的每个f
Set matches=re.Execute(f.Name)
如果匹配。计数>0,则
HM=小时(f.DateLastAccess)*100+分钟(f.DateLastAccess)

如果HM>=startTime和HM可能有一些用处。这里有很多东西我不完全理解,但这给了我一些想法和一些好的阅读:-)感谢您提供的这些信息,它应该可以帮助我更接近我的目标。当您使用WMI服务时,它可以让您将其连接到一个。现在,您可以处理真正基于事件的事件。在这里使用事件接收器将是首选方法(因为它使用的开销要少得多),但是,OP没有提供足够的信息来利用事件接收器。尽管听起来好像在一天中的特定时间使用u InstanceCreationEvent监视特定文件夹可能会完成预期任务。此u InstanceCreationEvent将监视在特定位置创建的文件?如果这就是它所做的,那将非常有帮助。我希望我能在我的帖子中更准确,但是我也没有得到关于请求的详细信息,我仍然在等待更多关于文件如何命名、何时传入、是否删除等的信息。。。能否展开InstanceCreationEvent?可以,InstanceCreationEvent可用于监视在特定位置创建文件的情况。您只需要知道文件路径。看到我的编辑上面。这是非常有帮助的:-),我要玩这个,看看我是否能掌握它如何流入其他东西,我想做的脚本,谢谢你的帮助!
Replace(filename,"*", "")
If inStr(file.Name, filename)<>0 Then
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