tags:

views:

1305

answers:

4

I am looking for a way to mount\unmount a USB flash drive using VBScript. This is the closest I was able to get.

Sub EjectDrive(strDrive)
On Error Resume Next
CONST SSF_DRIVES = 17
Set objShell = CreateObject("Shell.Application")
Set objDrive = objShell.Namespace(SSF_DRIVES).ParseName(strDrive)
objDrive.InvokeVerb "E&ject"
End Sub
+2  A: 

This will work on Windows Server 2003, but not NT/2000/XP/Vista unfortunately.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_Volume Where Name = 'E:\\'")

For Each objItem in colItems
    objItem.Dismount(True, True)
Next

From Dismount a Volume.

aphoria
A: 

Is there no solution for NT/2000/XP/Vista?

I couldn't find a way to do it with pure VBScript and WMI. If you're open to using a 3rd party executable or .DLL, there are options.
aphoria
A: 

You can popup the eject dialog by using something like this. I am not sure if it possible to unmount a specific device.

Set WshShell = WScript.CreateObject("WScript.Shell")
intReturn = WshShell.Run("RunDll32.exe shell32.dll,Control_RunDLL hotplug.dll", 1, TRUE)
Zoredache
A: 

Take a look at this thread, its talks about using the mountvol.exe command line tool to mount/unmount a drive, and it should work for USB flash drives, or there is also a program called deveject. Se this thread for more info: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/d2e5d16e-e7c9-48ef-88b8-3abf6e638384

mrTomahawk