Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

This question already has an answer here:

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.

share|improve this question
    
@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
add comment

marked as duplicate by magicandre1981, gronostaj, Mokubai, mpy, Shakehar Aug 30 '13 at 17:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 2 down vote accepted

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";
}
share|improve this answer
    
+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
show 7 more comments

Not the answer you're looking for? Browse other questions tagged or ask your own question.