Command line or WinBatch way to "Safely Remove" a USB drive...

Started by snowsnowsnow, June 25, 2020, 04:47:39 PM

Previous topic - Next topic

snowsnowsnow

I'm looking for a way to do the "Safely Remove" thing from the command line (or, to be maximally on-topic, to do it in WinBathc).

I seem to remember that there is a function in one of the extenders to do this, but I can't seem to remember it (or find it by grepping though my program).  Is there?

Alternatively, can anyone suggest another way to do it?  I googled it and there seem to be ways to do it in either "cscript" or "javascript", and these can, apparently, be invoked from the Windows Command Prompt.  But I'd rather do it in WB if possible; also I'd rather someone here could certify something to be working, rather than accept the word of some rando on the net.

As a last resort, I'd also be open to automating the Safely Remove dialog (you start it up via: control hotplug.dll), but I had problems getting RoboScripter to work with that applet.


Target OS is: Windows 7

stanl


ChuckC

PowerShell Code:

  $driveEject = New-Object -comObject Shell.Application
  $driveEject.Namespace(17).ParseName("E:").InvokeVerb("Eject")

References:

https://serverfault.com/questions/130887/dismount-usb-external-drive-using-powershell

http://sushihangover.blogspot.com/2012/02/powershell-eject-local-or-remote.html


Given that WinBatch can create and interact with COM objects, this should be absolutely trivial to convert to native WIL code.

snowsnowsnow

1) Stan: Not the point.  I know how to bring up the Safely Remove applet.  But then you'd have to write WB code to automate it.  That's the hard part.

2) Chuck: Yes, I found that two-liner in a google search, but have no idea what to do with it.  I know nothing about "Power Shell" or how to run it from within WB.

Can you help with that?

3) I still seem to remember that there was a function in one of the WB extenders for this.  Can anyone help with that?

ChuckC

As I said, the conversion is trivial:

; EjectDevice.wbt

obj = ObjectOpen("Shell.Application")

obj.Namespace(17).ParseName("D:").InvokeVerb("Eject")


In my case, my USB flash drive is "D:".  Simply parameterize that portion and you're good to go with making a UDF.

For further reading, check out "Shell.Application" in Microsoft's online docs at the MSDN site.

Also, it's well worth your time to take the time to do some learning about PowerShell.  It's been around over a decade and the 'Net is full of very helpful examples of how to get things done with it.  Many of those things that are being done with it are making use of WMI classes & objects, COM objects, .Net assemblies/classes/types and Win32 API functions, all of which can be adapted for use in a WinBatch script.  Getting proficient with at least reading and understanding it puts you more in the case of knowing how to be successful at fishing rather than having to ask if anybody can spare an extra fish :P

td

By using the Wil Type Viewer to examine the methods and properties of the "shell.application" COM Automation class, you could easily come up with something like the following:

Code (winbatch) Select
UsbDrive = 'G:'
ssfDRIVES = 17
objShell = ObjectCreate('shell.application')
objShell.NameSpace(ssfDRIVES).ParseName(UsbDrive).InvokeVerbEx('Eject')
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

snowsnowsnow

Quote
Code (winbatch) Select

; EjectDevice.wbt

obj = ObjectOpen("Shell.Application")

obj.Namespace(17).ParseName("D:").InvokeVerb("Eject")


Thank you ever so much for this.  Very helpful.  I would never have figure that out myself.  Again, thanks.

Not interested in PowerShell or whatever.  Just not the sort of thing I'm interested in learning about.  It just looks like gibberish.  I just needed this one task done.

Again, thanks.

ChuckC

PowerShell is actually a creature of rigorous structure and predictability once you understand the basic rules of the syntax that it uses.  I'll freely admit that it is more verbose than WIL, so the code for a PowerShell script will be more voluminous than a WinBatch script, but nothing near on the order of, say, COBOL vs. C.

As I said, it's well worth learning.  You indicated that you had already found the same PS code that I referenced, yet you didn't have the patience or interest in understanding it.  For a modest investment in learning, you open up a large library of source material that you can draw from when looking for functional examples of how to a perform a task and thus save yourself the time involve in asking a question and waiting for an answer before you can continue with what you're working on.


stanl

Quote from: snowsnowsnow on June 26, 2020, 07:07:45 AM
1) Stan: Not the point.  I know how to bring up the Safely Remove applet.  But then you'd have to write WB code to automate it.  That's the hard part.



My bad. I was just trying to answer the Command Line part. Otherwise, I would have suggested Powershell as Chuck did and Tony would have promptly converted to pure WB.


Powershell is not gibberish. The fact that it plays well with the WB CLR is a fantastic way to learn about .NET while still in WB. At work lately I am involved with tasks regarding access to several SFTP sites using pgp authentication keys. The company has authorized WinSCP and wouldn't you know it writes Powershell code for you for basic tasks. Call the code via the CLR with a WB dialog - trivial.