简体   繁体   中英

vbscript Get Copy-Paste Event from folder

I would like to run a vbscript file every time a user copy-pastes a file to a folder. I want to catch that event. Is this possible? Code snippets please. Thanks!

Monitoring Folder with Vbscript ==> MonitorFolder.vbs You can found your LogFile for monitoring on your %AppData% folder with the name of NameofYourFolder.log .

I hope that this script can help you ;)

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
'****************************************************************************

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM