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

时间:2016-07-18 20:58:48

标签: batch-file windows-subsystem-for-linux

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

我发现很多脚本都做了不同的部分,但没有一个能按我的意愿工作。

sombody可以帮帮我吗?

3 个答案:

答案 0 :(得分:1)

我在vbscript之前发布了你想要做的事情,看看并尝试一下!

Vbscript to copy files with specific extension from usb when plugged in

编辑于2016年7月19日@ 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 
'************************************************************************

答案 1 :(得分:0)

等待卷更改,然后将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

答案 2 :(得分:0)

试试这个:

@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"