2

大家早上好,我一直在尝试将脚本执行时从用户那里获取文件路径和文件名(其中可能包含通配符)的 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

4

3 回答 3

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

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

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

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

有关完整说明,您可以在我的博客上阅读监控和归档新创建的文件。

于 2012-10-25T04:34:24.367 回答
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

参考:

于 2017-04-28T06:10:00.760 回答
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
于 2015-04-18T13:21:27.757 回答