windows - 使用VB脚本监控目录

标签 windows vbscript

我使用下面的代码来捕获在目录中创建的文件:

strComputer = "."
strQueryFolder = Replace(strFolder, "\", "\\\\")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" &     strComputer & "\root\cimv2") 
Set colMonitoredEvents = objWMIService.ExecNotificationQuery ("SELECT * FROM __InstanceCreationEvent WITHIN " & nFrequency & " WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent='Win32_Directory.Name=""" & strQueryFolder & """'") 

Do 
    Set objLatestEvent = colMonitoredEvents.NextEvent
    strNewFile = objLatestEvent.TargetInstance.PartComponent
    arrNewFile = Split(strNewFile, "=")
    strFilePath = arrNewFile(1)
    strFilePath = Replace(strFilePath, "\\", "\")
    strFilePath = Replace(strFilePath, Chr(34), "")
    strFileName = Replace(strFilePath, strFolder, "")
    strTempFilePath = WScript.CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2) & "\TEMP.M4A"

    'Do something with strTempFilePath 

Loop

问题是脚本在将文件写入目录时捕获文件。

如何判断文件写入完成?

最佳答案

我记得我做了一个Vbscript这样的,你可以看一下这段代码

希望能够对您有所帮助,并给您带来更多想法。

Option Explicit
Dim fso,Message,Message2,Msg,intInterval,strDrive,strFolder,strComputer,objWMIService,strQuery
Dim colEvents,objEvent,objTargetInst,objPrevInst,objProperty,ws,LOG_FILE_PATH,LogFile,Chemin,MonTableau
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject("WScript.Shell")
strComputer = "." 
Chemin = Parcourir_Dossier()
MonTableau = Split(Chemin,"\")
LogFile = MonTableau(UBound(MonTableau)) & ".log"
LOG_FILE_Path = ws.ExpandEnvironmentStrings("%AppData%") & "\" & LogFile
intInterval = "2"
'****************************************************************************************************
Function Parcourir_Dossier()
    Dim ws,objFolder,Copyright
    Copyright = "[ © Hackoo © 2014 ]"
    Set ws = CreateObject("Shell.Application")
    Set objFolder = ws.BrowseForFolder(0,"Choose the folder to watch for "_
    & Copyright,1,"c:\Programs")
    If objFolder Is Nothing Then
        Wscript.Quit
    End If
    Parcourir_Dossier = objFolder.self.path
end Function
'****************************************************************************************************
Chemin = Split(fso.GetAbsolutePathName(Chemin),":")
strDrive  = Chemin(0) & ":"
strFolder = Replace(Chemin(1), "\", "\\")
If Right(strFolder, 2) <> "\\" Then strFolder = strFolder & "\\"
'Connexion au WMI
Set objWMIService = GetObject( "winmgmts:" &_ 
"{impersonationLevel=impersonate}!\\" &_ 
strComputer & "\root\cimv2" )
'La chaîne de la requête
strQuery =  _
"Select * From __InstanceOperationEvent" _
& " Within " & intInterval _
& " Where Targetinstance Isa 'CIM_DataFile'" _
& " And TargetInstance.Drive='" & strDrive & "'"_
& " And TargetInstance.path='" & strFolder & "'"
'Exécutez la requête
Set colEvents = _
objWMIService.ExecNotificationQuery(strQuery)  
Do 
    Set objEvent = colEvents.NextEvent()
    Set objTargetInst = objEvent.TargetInstance
    Select Case objEvent.path_.Class 
'Si c'est le cas de la création de fichier ou d'un événement de suppression et afficher
'juste le nom du fichier
    Case "__InstanceCreationEvent" 
        Message = DblQuote(objTargetInst.Name) & " is created !"
        Message2 = String(10,"*") & Now & String(10,"*") & vbCrLf & Message & vbCrLf & String(70,"*")
        Call Log(LOG_FILE_Path,Message2)
        'MsgBox Message2,VbInformation,Message
    Case "__InstanceDeletionEvent" 
        Message = DblQuote(objTargetInst.Name) & " is deleted !"
        Message2 = String(10,"*") & Now & String(10,"*") & vbCrLf & Message & vbCrLf & String(70,"*")
        Call Log(LOG_FILE_Path,Message2)
        'MsgBox Message2,VbInformation,Message
'Si c'est le cas de la modification du fichier,comparer les valeurs de propriété de la cible et de l'instance précédente
'et afficher les propriétés qui ont été changé comme la taille et LastModified
    Case "__InstanceModificationEvent" 
        Set objPrevInst = objEvent.PreviousInstance
        For Each objProperty In objTargetInst.Properties_
            If objProperty.Value <> _
            objPrevInst.Properties_(objProperty.Name) Then
            Message = "modified file :        " & vbCrLf &_
            objTargetInst.Name & vbCrLf &_
            "Property :       "_
            & objProperty.Name & vbCrLf &_
            "Last Value : "_
            & objPrevInst.Properties_(objProperty.Name) & vbCrLf &_
            "New value :      " _
            & objProperty.Value
            Message2 = String(10,"*") & Now & String(10,"*") & vbCrLf & Message & vbCrLf & String(70,"*")
            Call Log(LOG_FILE_Path,Message2)
            'MsgBox Message,64,DblQuote(objTargetInst.Name)
        End If    
    Next
End Select 
Loop
'**********************************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
Sub Log(strLogFileChemin,strLogContent)
Const APPEND = 8
Dim objFso,objLogFile
Set objFso = CreateObject("Scripting.FileSystemObject")
If Not objFso.FileExists(strLogFileChemin) Then objFso.CreateTextFile(strLogFileChemin, True).Close
Set objLogFile = objFso.OpenTextFile(strLogFileChemin,APPEND)
objLogFile.WriteLine strLogContent
objLogFile.Close
End Sub 
'**********************************************************************************************

关于windows - 使用VB脚本监控目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25509587/

相关文章:

windows - Windows Powershell 中的 Unix tail 等效命令

vba - GNUPLOT 与 VBA - STDERR 去了哪里?

vbscript - 使用 VBScript 获取 Mac 地址

windows - RegDeleteKey 和 RegDeleteKeyEx

xml - 用于创建本地用户并将详细信息写入 XML 文件的 Powershell 脚本

windows - 如何使用 Win32 API 与 com 端口 (RS232) 通信

windows - 启动 Windows 驱动程序开发

VBScript 无法运行 - 错误 "Expected Statement"

vbscript - 权限被拒绝错误 800A0046 'objIE.Document.parentWindow.screen'

vbscript - 如何使用 GUI 脚本读取 GuiTree 控件的行详细信息