-1

This is my first .bat file. I want to make a script to copy all my files from USB or DVD/CD when volume is detected to hardisk.

I did not find something good. I think I need to implement something like this:

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

My final result:

: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

I don't know how to write after NEQ, because if no USB is connected cmd print No Instance(s) Available., but don't work correct for what I want.

Please help me... what do I miss?

GameZone RO
  • 63
  • 1
  • 8
  • 1
    Since you already know what to implement, you should just try it; when you are stuck at a certain point, come back here, clearly describing the failure you are facing. Hint: to get the drive type, you could use the [`wmic` command](http://ss64.com/nt/wmic.html), like `wmic LogicalDisk get Description,DeviceID,DriveType`... – aschipfl Dec 12 '16 at 13:20
  • 1
    You shoud try this vbscript instead [Script that detect usb when it is inserted and copy files from usb to computer up](http://stackoverflow.com/questions/38445884/script-that-detect-usb-when-it-is-inserted-and-copy-files-from-usb-to-computer?answertab=active#tab-top) – Hackoo Dec 12 '16 at 14:02

1 Answers1

0

This waits for a VolumeChangeEvent then copies c:\test onto the new volume.

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
  • This must be a new version of Batch that I've never seen before. Is it included in Windows 10? – Thomas Weller Dec 12 '16 at 18:30
  • @ThomasWeller I think it's VBScript Error: http://imgur.com/Jf1oW99 I don't know VBScript or Batch, but Batch looks more simple. Help... – GameZone RO Dec 12 '16 at 18:38
  • That because you don't have a `c:\test` folder that it will copy files to. Create it. Batch can't wait for events. –  Dec 12 '16 at 19:21
  • @Noodles it's correct. I change `objitem.DriveType = 2` to `objitem.DriveType = 5` for CD-ROM but don't work. Why? – GameZone RO Dec 12 '16 at 19:37
  • Because you are only changing discs perhaps. Try a second script monitoring the drive letter, see http://stackoverflow.com/questions/36517836/trigger-a-vbscript-when-a-file-is-added-to-a-folder –  Dec 12 '16 at 19:42
  • @Noodles Work only if I connect USB, then copy from CD-ROM and USB (both). I read your post from link, but I don't understand too much. – GameZone RO Dec 12 '16 at 19:45
  • VBScript. You cannot do this in batch unless you want to loop testing for when the root directory is available - See `if /?` as in `if exist G:\`, `goto /?`, and `timeout /?`. –  Dec 12 '16 at 19:48