1

I'm trying to eject my flash drive when i type eject into a command prompt, i have a shell of a code, but i don't have the actual code to eject it.

UNLOCK

@echo off
echo '
set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p

if %password%==Eject goto EJECT

:EJECT

^if EXIST "D:\Some file in the Flash drive" goto D-DRIVE

:D-DRIVE

^code that ejects the D drive

after that bit i need the eject code

aphoria
  • 19,796
  • 7
  • 64
  • 73
  • 1
    How about: SuperUser:[Remove USB device from command line](http://superuser.com/questions/443162/remove-usb-device-from-command-line) – mojo Dec 01 '14 at 15:39
  • 1
    You can also use - http://www.nirsoft.net/utils/nircmd.html – Leptonator Dec 01 '14 at 18:10

1 Answers1

2

I was about to comment that same thing that mojo did above at about the same time. However, after testing, it seems that scripting diskpart to dismount my own removable drives prevents them from automatically re-acquiring a drive letter the next time they're plugged in. I had to go to Disk Management and re-assign a drive letter when re-plugging. I'm sure that's not what you want. Scripting a dismount with Win32_Volume like this guy did had the same unfortunate side effect.

Playing with hotplug.dll like this guy did wasn't much help either, with Windows reporting that the type of drive wasn't a removable drive.

No, the most reliable, least side-effect-y method that doesn't require 3rd party utilities is to invoke the "Eject" verb from a "My Computer"-ish context. Here's a solution involving a batch / JScript hybrid script:

@if (@a==@b) @end   /* JScript ignores this multiline comment

:: dismount.bat
:: safely dismounts all removable drives

@echo off
setlocal

cscript /nologo /e:JScript "%~f0"

goto :EOF

:: end batch portion / begin JScript portion */

// DriveType=1 means removable drive for a WScript FSO object.
// See http://msdn.microsoft.com/en-us/library/ys4ctaz0%28v=vs.84%29.aspx

// NameSpace(17) = ssfDRIVES, or My Computer.
// See http://msdn.microsoft.com/en-us/library/windows/desktop/bb774096%28v=vs.85%29.aspx

var oSH = new ActiveXObject('Shell.Application'),
FSO = new ActiveXObject('Scripting.FileSystemObject'),
removableDriveType = 1,
ssfDRIVES = 17,
drives = new Enumerator(FSO.Drives);

while (!drives.atEnd()) {
    var x = drives.item();
    if (x.DriveType == removableDriveType) {
        oSH.NameSpace(ssfDRIVES).ParseName(x.DriveLetter + ':').InvokeVerb('Eject');
        while (x.IsReady)
            WSH.Sleep(50);
    }
    drives.moveNext();
}

Also, you can invoke the "Eject" verb from the same namespace using PowerShell in a similar way, but you'll still need to Start-Sleep to prevent the ps thread from exiting before the drive has been ejected. I leave that as an exercise for the O.P. if he prefers PowerShell over WScript.


Edit: If you want to define a specific drive to dismount in your batch script, do it like this:

@if (@a==@b) @end   /*

@echo off
setlocal

set "drive=F:"

cscript /nologo /e:JScript "%~f0" "%drive%"

goto :EOF

::  */

var oSH = new ActiveXObject('Shell.Application'),
    FSO = new ActiveXObject('Scripting.FileSystemObject'),
    drive = WSH.Arguments(0);

oSH.NameSpace(17).ParseName(drive).InvokeVerb('Eject');
while (FSO.GetDrive(drive).IsReady) WSH.Sleep(50);
Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101
  • Is there a way to do it in VBScript or is java the only way? i prefer simply way to eject, if that's the simplest way then i'll take it. – Jonathan Viju Mathai Dec 01 '14 at 20:11
  • That's not Java. It's not even JavaScript, although that'd be a closer association. It's JScript, a close cousin to VBScript. Yes, it [can be done with VBScript](http://stackoverflow.com/q/14220273/1683264). I just used JScript because it's much more graceful to run a JScript snippet as a batch hybrid than it is to echo chunks of VBscript out to a separate vbs file. No this is not the only way. It can actually be done with much fewer lines of JScript code as well if I just arbitrarily `WSH.Sleep(1000)` or similar, but that might cause issues if there's an open handle blocking the Eject action. – rojo Dec 01 '14 at 20:30
  • what is the ending for JScript – Jonathan Viju Mathai Dec 02 '14 at 00:23
  • Nevermind that last comment, how would you make it eject just the F drive – Jonathan Viju Mathai Dec 02 '14 at 00:28
  • I don't know it I'm being stupid or something, but it's not working. can you give me a base code or something to help – Jonathan Viju Mathai Dec 05 '14 at 02:02
  • @JonathanVijuMathai See edited answer. This will be the last update I perform on this answer, as any further revisions are likely to be too specific to be of any benefit to future users. S.O. is not intended to be used as a free coding service. – rojo Dec 05 '14 at 15:06