Drive Detection

RandomCoder

Well-Known Member
Licensed User
Longtime User
Hi guys,

Been away for a while but just gettting back into some programming again.
Found a little tool for copying files and folders called ROBOCOPY, like the old DOS XCOPY but better (see here... Robocopy), and I'm wanting to automate this with B4PPC.

What I would like to know is if it is possible to detect when a USB drive has been plugged in?

The best I have come up with is to run a DirExist("MyBackup.etc") periodically to check if the USB drive is present but this is very hit and miss.
I'd much rather have some way of detecting that the drive has just been plugged in - is it possible?

Regards,
RandomCoder
 

agraham

Expert
Licensed User
Longtime User
This uses my (unpublished) Window Message libray but it is based on the same code as Dimitris used for dzEventsMagic. Make a dzEventMagic object called WMevent and change the event name to WMevent_MagicEvent.

Note that you get multiple messages, hence the check for a time window. You will have to track whether the device is present or not. Also note that you may need a delay after getting the event before the device is visible. You will have to play with this for yourself.

This event may happen for other things than a USB device. This works on my desktop for USB sticks and should work on a device with a USB port but for what range of devices I have no idea.


B4X:
Sub Globals
   'Declare the global variables here.
   WM_DEVICECHANGE = 537 ' 0x0219
   TimeChanged = Now
End Sub

Sub App_Start
   Form1.Show
   WMevent.New1("Form1", false)
   WMevent.Hook(WM_DEVICECHANGE)
End Sub


Sub Form1_Close
   WMevent.UnHook(WM_DEVICECHANGE)   
End Sub

Sub WMEvent_WmEvent
   msg = WMevent.wParam
   If msg = 7 AND Now > TimeChanged + cTicksPerSecond*3 Then
      TimeChanged = Now
      Msgbox("Device added or removed")
   End If
End Sub
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Agraham your amazing..... is there anything you don't know?

I've not tried it yet but have every faith it will work.

Thankyou very much,
RandomCoder
 

agraham

Expert
Licensed User
Longtime User
Further investigation shows that you only get one each of events with these wParam values on a change. There may still be a delay before the device is accessible. Again you need to change the Sub name to WMevent_MagicEvent for the dzEventsMagic library.


B4X:
Sub WMEvent_WmEvent
   msg = WMevent.wParam
   Select msg
      Case 32772:   
         Msgbox("Device removed")
      Case 32768
         Msgbox("Device added")
   End Select   
End Sub
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
I like the twist on the ROBOCOPY name... Robscopy ;)

If you wouldn't mind then it would be nice to see your source code as there are some things you have done different such as using a TreeView to select paths and also the ability to exclude files (not just folders).

My app is intended to run slightly differently in that I'd like to have it running in the background all of the time and then detect when x amount of time has passed (set by the user) and trigger the ROBOCOPY function, if the destination is a USB device it will wait for the device to be connected.

Attached is my source code so that you can see the direction I've taken. This is no where near finished and does not include running in the background which needs to be added now that Agraham and DZT have been so kind as to show me a way forward. Most of my programming is done during a 30 minute meal break at work :sign0148:
I've commented out the SHELL comand and just use a label to display the arguments for the time being.

Regards,
RandomCoder
 

Attachments

  • Backup Utility.zip
    2.1 KB · Views: 212
Top