-1

I would like to be able to programmatically safely remove a USB drive, given a drive letter. This is actually to be implemented in an application, but I figured that if it can be done using a batch script, I can easily transfer it to code.

The only twist is that I do not want to include any third party applications or anything, so if anyone can show me how to use diskpart or anything alternative I would be grateful.

Andy
  • 481
  • 4
  • 12
  • 21
  • @moses Not exactly, because I'm asking for a batch script and I'll have to determine which drive number I need to use programatically – Andy Aug 28 '13 at 19:24
  • Does it have to be a batch script, or would a powershell (included by default in Vista+, installable via windows update in XP) script be acceptable instead? – Scott Chamberlain Aug 28 '13 at 19:25
  • @ScottChamberlain Well, I'll need to replicate whatever I get in my Java application. I know I could do that with a batch script, but I could probably do it with a powershell script too... I'll look into it now though, one minute – Andy Aug 28 '13 at 19:28

1 Answers1

2

If Powershell is acceptable for your the batch scripting language, it is as simple as

$vol = get-wmiobject -Class Win32_Volume | where{$_.Name -eq 'F:\'}  
$vol.DriveLetter = $null  
$vol.Put()  
$vol.Dismount($false, $false)

EDIT:
Seeing your comment that you will be doing this through a Java application, you may just want to invoke the WMI object directly from Java.

Java Example:

public string GenerateScript(string driveLetter)
{
    return "$vol = get-wmiobject -Class Win32_Volume | where{$_.Name -eq '" + driveLetter + :\\'};\n" +  
           "$vol.DriveLetter = $null\n" +  
           "$vol.Put()\n" +  
           "$vol.Dismount($false, $false)\n";
}
Scott Chamberlain
  • 30,694
  • 7
  • 96
  • 109
  • +1 Thanks for your answer. Looks like it will do the job perfectly because I can invoke powershell through Java using multiple ways, so it's fine it isn't a bat! – Andy Aug 28 '13 at 19:40
  • Just one question, how could this code be editted so that I could save it as a script and pass the drive letter in? – Andy Aug 28 '13 at 19:49
  • Just replace the `F` in `'F:\'` with whatever drive letter you want instead of `F`, so some kind of variable and string concatenation. – Scott Chamberlain Aug 28 '13 at 19:50
  • I plan to invoke the script from Java though and the drive letter won't always be the same – Andy Aug 28 '13 at 19:52
  • I gave you some code, I have no idea if that is valid java, I am more of a C# guy myself. – Scott Chamberlain Aug 28 '13 at 19:57
  • Ah, I see what you've done. It looks pretty ok to me, but even if it isn't fully functional I can work with that. Thanks again. – Andy Aug 28 '13 at 20:04
  • I've just discovered that Powershell is not enabled by default (the execution of scripts), and so unfortunately this is not a suitable solution after all – Andy Aug 30 '13 at 15:25
  • @Andy There is a command line option when you launch powershell.exe for powershell to choose restriction of execution of scripts, I don't remember it off of the top of my head though. – Scott Chamberlain Aug 30 '13 at 15:49
  • Ah ok, thank you. Think I've found the command line option now - it's `ExecutionPolicy` – Andy Aug 30 '13 at 16:30
  • Sorry to be a pain; I'm now getting an Exception for `Calling "Put" with "0" arguments` – Andy Aug 30 '13 at 19:03
  • I got that error too in my testing, It looks like it had to be run as administrator. But even then I was having trouble, You may want to search for another solution. However I think you should open a new question, this is a [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), you asked "*How do I run a Safely Remove Hardware from a batch script?*" when you should have asked "*How do I run Safely Remove Hardware from Java? I have tried running from a batch script but I can't get it to work*" (You should be asking this on Stack Overflow) – Scott Chamberlain Aug 30 '13 at 19:07
  • I have already done so, this was a follow up question from here: http://stackoverflow.com/questions/18472975/eject-usb-drive-from-java – Andy Aug 30 '13 at 19:33