VBScript - Unmount USB drive

user736887 picture user736887 · Nov 21, 2011 · Viewed 7.7k times · Source

I have a tedious project coming up. I need to insert an USB flash drive into a computer, then copy over three files to that drive, and then unmount it and repeat 3000 times (literally). I was hoping to come up with some VBScript that can reduce my actions to just

  1. insert the USB flash drive,
  2. double click on the .vbs file,
  3. remove the USB flash drive.

I figure it isn't too difficult to come up with the copy and paste part of the code as long as I am inserting the USB into the same port every time. Is this assumption correct? However, the real problem is unmounting/ejecting the USB drive. Is there any simple VB Script code that can accomplish this?

Answer

josh3736 picture josh3736 · Nov 21, 2011

This was the first Google result for vbscript unmount: Unmounting USB drives


This worked on Windows 7 if the script is run elevated (as an Administrator):

Set shell = WScript.CreateObject("WScript.Shell")
shell.Run "mountvol <drive>: /d"

mountvol is included with Windows.


You could probably even reduce the input needed by polling the drive letter that your USB sticks mount to, and if there is a drive present, copy the files over and then unmount.