插入USB时检测USB并将文件从USB复制到计算机的脚本

Script that detect usb when it is inserted and copy files from usb to computer

我正在尝试编写一个 windows 批处理脚本,它将一直 运行 当插入 USB 闪存驱动器时,它会将文件从 USB 复制到计算机。

我找到了很多脚本来完成它的不同部分,但其中 none 可以按我的意愿工作。

有人可以帮助我吗?

这会等待卷更改,然后将 USB 复制到 c:\test。许多消息框,因此您可以看到正在发生的事情。删除它们以进行生产。

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

                        Wscript.Echo "Starting Copying"
                        Set objShell = CreateObject("Shell.Application")
                        Set Ag=Wscript.Arguments
                        set WshShell = WScript.CreateObject("WScript.Shell")

                        Set SrcFldr=objShell.NameSpace(objitem.driveletter)
                        Set DestFldr=objShell.NameSpace("c:\test\")
                        Set FldrItems=SrcFldr.Items
                        DestFldr.CopyHere FldrItems, &H214
                        Wscript.Echo "Finished Copying"


               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

我在这里发布了一个 vbscript 来做你想做的事情,看看并试试吧!

编辑于 19/07/2016 @10:42:

我以管理员身份将此 vbsript 改进为 运行,并只执行此脚本的一个实例。

AutoSave_USB_SDCARD.vbs复制到我的文档文件夹

Option Explicit
' Run as Admin
If Not WScript.Arguments.Named.Exists("elevate") Then
    CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , WScript.ScriptFullName & " /elevate", "", "runas", 1
    WScript.Quit
End If
' To let executing just one insctance of this script
If AppPrevInstance() Then 
    MsgBox "There is an existing proceeding !" & VbCrLF &_
    CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"    
    WScript.Quit   
Else
    Do
        Call AutoSave_USB_SDCARD()
        Pause(30)
    Loop
End If
'**************************************************************************
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
'*************************AutoSave_USB_SDCARD()****************************
Sub AutoSave_USB_SDCARD()
    Dim Ws,WshNetwork,NomMachine,MyDoc,strComputer,objWMIService,objDisk,colDisks
    Dim fso,Drive,NumSerie,volume,cible,Amovible,Dossier,chemin,Command,Result
    Set Ws = CreateObject("WScript.Shell")
    Set WshNetwork = CreateObject("WScript.Network")
    NomMachine = WshNetwork.ComputerName
    MyDoc = Ws.SpecialFolders("Mydocuments")
    cible = MyDoc & "\"
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
    Set colDisks = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_LogicalDisk")

    For Each objDisk in colDisks
        If objDisk.DriveType = 2 Then
            Set fso = CreateObject("Scripting.FileSystemObject")
            For Each Drive In fso.Drives
                If Drive.IsReady Then
                    If Drive.DriveType = 1 Then
                        NumSerie=fso.Drives(Drive + "\").SerialNumber
                        Amovible=fso.Drives(Drive + "\")
                        Numserie=ABS(INT(Numserie))
                        volume=fso.Drives(Drive + "\").VolumeName
                        Dossier=NomMachine & "_" & volume &"_"& NumSerie
                        chemin=cible & Dossier
                        Command = "cmd /c Xcopy.exe " & Amovible &" "& chemin &" /I /D /Y /S /J /C"
                        Result = Ws.Run(Command,0,True)
                    end if
                End If   
            Next
        End If   
    Next
End Sub
'**************************End of AutoSave_USB_SDCARD()*******************
Sub Pause(Sec)
    Wscript.Sleep(Sec*1000)
End Sub 
'************************************************************************

试试这个:

@echo off
set backupcmd=xcopy /s /c /d /e /h /i /r /y /
%backupcmd% "%USERPROFILE%\Pictures" "%drive%\all\My pics"
%backupcmd% "%USERPROFILE%\Favorites" "%drive%\all\Favorites"
%backupcmd% "%USERPROFILE%\Videos" "%drive%\all\Vids"
%backupcmd% "%USERPROFILE%\Documents" "%drive%\all\Docs"
%backupcmd% "%USERPROFILE%\OneDrive" "%drive%\all\Onedrive"
%backupcmd% "%USERPROFILE%\Desktop" "%drive%\all\Desktop"
%backupcmd% "%USERPROFILE%\Network" "%drive%\all\Other devices"