插入USB时检测USB并将脚本从USB复制到计算机的脚本 [英] Script that detect usb when it is inserted and copy files from usb to computer

查看:130
本文介绍了插入USB时检测USB并将脚本从USB复制到计算机的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write a windows batch script that will run all the time and when a usb flash drive will be inserted it will copy files from usb to computer.

我发现有很多脚本可以完成它的不同部分,但是没有一个可以按照我的意愿工作.

I've found a lot of script that do different parts of it but none of them works as I want.

有人可以帮助我吗?

推荐答案

我在此处的vbscript之前发布了您想要执行的操作,只是看一下并尝试一下即可!

I posted before a vbscript here to do what you want just take a look and try it !

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

Edit on 19/07/2016 @10:42 :

我改进了该vbsript,使其以管理员身份运行,并允许仅执行该脚本的一个实例.

I improved this vbsript to run as admin, and to let executing just one insctance of this script.

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

AutoSave_USB_SDCARD.vbs to copy into My Documents folder

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

这篇关于插入USB时检测USB并将脚本从USB复制到计算机的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆