如何使用cmd编程从flashdrive自动运行.bat文件?

时间:2016-07-18 03:35:50

标签: batch-file cmd

我正在寻找一种方法,只要将闪存驱动器插入计算机,就可以从闪存驱动器运行名为File.bat的.bat文件。我目前正在运行Windows 10,我正在使用notepad ++和cmd。由于出于安全原因,在Windows 7中停止了Autorun.inf功能,因此这不是一种选择。我也可以使用U3闪存驱动器,但我正在寻找一些我可以编写或试验的东西,而不是特别购买。我不是cmd的专家,我不知道它的许多功能,但我知道足够被认为是初学者,如果不是新手。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
Set evtDevice = objWMIService.ExecNotificationQuery ("SELECT * FROM Win32_VolumeChangeEvent")

Wscript.Echo "Waiting for events ..."
Do
    Set objReceivedEvent = evtDevice.NextEvent
    'report an event
    Wscript.Echo " Win32_Device Changed event occurred" & VBNewLine
    If objReceivedEvent.EventType = 1 Then 
         Wscript.Echo "Type = Config Changed" 
    ElseIf objReceivedEvent.EventType = 2 Then 
         Wscript.Echo "Type = Device Arrived" 

         Set colItems = objWMIService.ExecQuery("Select * From Win32_Volume")
         For Each objItem in colItems
               Wscript.Echo objitem.DriveType
               If objitem.DriveType = 2 then
                        Wscript.Echo objItem.DriveType & " " & objItem.Name & " " & objItem.driveletter
                End If
        Next
    ElseIf objReceivedEvent.EventType = 3 Then 
         Wscript.Echo "Type = Device Left" 
    ElseIf objReceivedEvent.EventType = 4 Then 
         Wscript.Echo "Type = Computer Docked" 
    End If
Loop

等待卷更改。

答案 1 :(得分:0)

创建一个autorun.inf

中写下来
[autorun]
open=a. bat