-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
  • 13
  • 21

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";
}