VBscript 在特定时间范围内检查文件是否存在(具有使用通配符的能力) [英] VBscript to check for the existance of a file (with the ability to use a wildcard) within a certain time frame

查看:32
本文介绍了VBscript 在特定时间范围内检查文件是否存在(具有使用通配符的能力)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 exicuted. The script will then check the specified directory for a file that matches the provided file name and then looks at the last modified date to see if it was created/modified within a certain time frame (i.e. 6am plus or minus 5 minutes). It would then copy said file into a zip file.

到目前为止,我已经能够使参数起作用,并且我已经设置为获取当前时间,查看文件夹中的文件并将硬编码文件名与文件夹中的文件名匹配.这就是我迄今为止所拥有的.

So far I have been able to get the arguments working, and I have it setup to grab the current time, look at the files in the folder and match a hard coded filename to one in the folder. This is what I have thus far.

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 菜鸟,我期待着学习方法!

I am a VBscript noob and I am looking forward to learning the way!

第三章

推荐答案

如果使用 WMI,它支持通配符.

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

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

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.

这篇关于VBscript 在特定时间范围内检查文件是否存在(具有使用通配符的能力)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆