簡體   English   中英

vbscript從文件夾獲取復制粘貼事件

[英]vbscript Get Copy-Paste Event from folder

每當用戶將文件復制粘貼到文件夾中時,我都希望運行一個vbscript文件。 我想趕上那件事。 這可能嗎? 請提供代碼段。 謝謝!

使用Vbscript ==> MonitorFolder.vbs監視文件夾您可以在名稱為NameofYourFolder.log的 %AppData%文件夾中找到要監視的LogFile。

我希望這個腳本可以為您提供幫助;)

Option Explicit
If AppPrevInstance() Then   
    MsgBox "There is an existing instance !" & VbCrLF & CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing instance !"   
    WScript.Quit   
Else  
    Call MonitorFolder()
end if
'*****************************************************************************************************
Sub MonitorFolder()
    Dim fso,Message,Message2,Msg,intInterval,strDrive,strFolder,strComputer,objWMIService,strQuery
    Dim colEvents,objEvent,objTargetInst,objPrevInst,objProperty,ws,LOG_FILE_PATH,LogFile,PathFolder,MonTableau
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ws = CreateObject("WScript.Shell")
    strComputer = "." 
    PathFolder = Browse4Folder
    MonTableau = Split(PathFolder,"\")
    LogFile = MonTableau(UBound(MonTableau)) & ".log"
    LOG_FILE_Path = ws.ExpandEnvironmentStrings("%AppData%") & "\" & LogFile
    intInterval = "2"
    PathFolder = Split(fso.GetAbsolutePathName(PathFolder),":")
    strDrive  = PathFolder(0) & ":"
    strFolder = Replace(PathFolder(1), "\", "\\")
    If Right(strFolder, 2) <> "\\" Then strFolder = strFolder & "\\"
'Connecting to WMI
    Set objWMIService = GetObject( "winmgmts:" &_ 
    "{impersonationLevel=impersonate}!\\" &_ 
    strComputer & "\root\cimv2" )
'The query string
    strQuery =  _
    "Select * From __InstanceOperationEvent" _
    & " Within " & intInterval _
    & " Where Targetinstance Isa 'CIM_DataFile'" _
    & " And TargetInstance.Drive='" & strDrive & "'"_
    & " And TargetInstance.path='" & strFolder & "'"
'Run Query
    Set colEvents = _
    objWMIService.ExecNotificationQuery(strQuery)  
    Do 
        Set objEvent = colEvents.NextEvent()
        Set objTargetInst = objEvent.TargetInstance
        Select Case objEvent.path_.Class 
'If this is the case of file creation or deletion event and display just the file name
        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
'If this is the case of the modification of the file, compare the property values of the target and the previous instance
'and view the properties that have been changed as the size and 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
End Sub
'**********************************************************************************************
Sub Log(strLogFilePathFolder,strLogContent)
Const APPEND = 8
Dim objFso,objLogFile
Set objFso = CreateObject("Scripting.FileSystemObject")
If Not objFso.FileExists(strLogFilePathFolder) Then objFso.CreateTextFile(strLogFilePathFolder, True).Close
Set objLogFile = objFso.OpenTextFile(strLogFilePathFolder,APPEND)
objLogFile.WriteLine strLogContent
objLogFile.Close
End Sub 
'****************************************************************************
'Checks whether a script with the same name as this script is already running
Function AppPrevInstance()   
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
    With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
        " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
        AppPrevInstance = (.Count > 1)   
    End With   
End With   
End Function       
'**************************************************************************
Function CommandLineLike(ProcessPath)   
ProcessPath = Replace(ProcessPath, "\", "\\")   
CommandLineLike = "'%" & ProcessPath & "%'"   
End Function
'**************************************************************************
'Function to add the double quotes into a variable
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'***************************************************************************
Function Browse4Folder()
Dim ws,objFolder,Copyright
Copyright = "  [ by Hackoo 2015 ]"
Set ws = CreateObject("Shell.Application")
Set objFolder = ws.BrowseForFolder(0,"Choose a folder for monitoring it"_
& Copyright,1,"c:\Programs")
If objFolder Is Nothing Then
    Wscript.Quit
End If
Browse4Folder = objFolder.self.path
end Function
'****************************************************************************

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM