gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:45:56 27 4
gpt4 key购买 nike

大家早上好 我一直在尝试将一个 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 菜鸟,我期待学习方法!

第 3 章

最佳答案

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

编辑............

您可以使用带有 __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在我的博客上。

关于windows - VBscript 在特定时间范围内检查文件是否存在(能够使用通配符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13052900/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com