批量文件从USB / DVD自动复制文件到HDD - 等待插入USB / DVD(检测到它)

时间:2016-12-12 13:05:09

标签: windows batch-file cmd

这是我的第一个.bat文件。我想创建一个脚本,以便在检测到卷时将所有文件从USB或DVD / CD复制到硬盘。

我没有找到好的东西。我想我需要实现这样的事情:

while(!volume:e /*if is DVD*/ || !volume:g /*if is USB*/)
    keep seeking
if(volume:e is detected)
xcopy e:\* d:\Backup\ /s /q
if(volume:g is detected)
xcopy g:\* d:\Backup /s /q

我的最终结果:

:while
    if ("wmic logicaldisk where drivetype= '2' get volumename") NEQ "No Instance(s) Available."::HERE IS THE PROBLEM
(GOTO :Syntax) else (GOTO :while)
:Syntax
    xcopy e:\* d:\Backup\ /s /q

//Use DriveType=5 for DVDs

我不知道如何在NEQ之后写字,因为如果没有USB连接cmd打印No Instance(s) Available.,但不能正确处理我想要的内容。

请帮助我...我错过了什么?

1 个答案:

答案 0 :(得分:0)

等待VolumeChangeEvent然后将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
               If objitem.DriveType = 2 then
                        Wscript.Echo objItem.DriveType & " " & objItem.Name & " " & objItem.driveletter

                        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