0

What is the best way to do the following: When I connect my device (let it be specific device - say Google Glass) to the USB port, I'd like that some agent will immediately pop-up (like windows autoplay), show me the list of files I currently have in the device, let me pick which one to upload to an ftp server, and at the end some "Upload" button in order to upload the chosen files to the ftp I would pre-define it. I also would like that after the user picks the files, it will rename the filesnames according to a pre-defined rule and only after that will upload it to the server.

Is there a way to do this? is there a tool that already does this or something similar?

I alreay wrote a .bat file with a script that can do the renaming and the uploading, so if there's some way to run the script when I press the "Upload" button it would be great.

Roi Bueno
  • 99
  • 10

1 Answers1

2

If you start the script with cscript it will write to a console rather than message boxes.

cscript <path to script>

E.G.

cscript "c:\somefolder\DeviceArrival.vbs"

Removable drives are drivetype=2. Create C:\Test first. Note I've changed the event type from all devices to just add/remove a drive.

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

Here's sample script that waits for devices to arrive/leave.

Note it runs twice so you may want to improve it because there are two notifications for each arrival. Also there needs to be checks done eg if you plug in one USB stick while another is plugged in both will be copied.

Noodles
  • 1,981
  • 1
  • 11
  • 4
  • In which language is it? Visual Basic? – Roi Bueno Sep 30 '14 at 17:06
  • VBScript. So you can use in VB6 or vbscript (although vb6 doesn't have echo command - use msgbox) – Noodles Sep 30 '14 at 18:39
  • So paste into a new notepad document and save as DeviceEvent.vbs – Noodles Sep 30 '14 at 18:43
  • Great, it does recognize the USB when connected. Now, is there a way to add a few lines to this script so that when USB connected it will also copy files from a specific folder inside the USB and paste them into some location in my hard drive? and if also know some commands in VBScript to upload the files to an ftp it would be the best. Thanks a lot!! – Roi Bueno Sep 30 '14 at 19:46
  • Well yes, but you said you already wrote that. Your first problem is to find out what event occurred (it might be a camera). `wmic volume get Caption, Capacity, freespace /format:table` list available drives (and you can do it in vbscript). To parse a command's output `for /f "tokens=1* delims=," %A in ('wmic volume get driveletter /format:csv') do echo %A %B` – Noodles Sep 30 '14 at 19:58
  • Thanks again for your generous help! I already have a batch file that does the copy, renaming and ftp upload job, as I said. So - if you can edit the script above to one that just waits for USB to be connected (like the one above) and instead of the echo's, runs the .batfile - it will do the job too.. – Roi Bueno Oct 02 '14 at 03:54
  • I have to go to hospital urgently tomoz. I've been at work and have to go again next three days as well, measuring people's reaction to terrorism threats. This is how to start a batch. `Set WshShell = WScript.CreateObject("WScript.Shell") msgbox FormatNumber(WshShell.Run("cmd /k dir c:\windows\*.*", 0, false)) `. There's some logic work that needs to be done - I'll update monday. – Noodles Oct 02 '14 at 14:49