VBScript - Unmount USB drive - vbscript

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
insert the USB flash drive,
double click on the .vbs file,
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?

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.

Since you're doing this with a thumbdrive, you can put DevCon on it and use DevCon to eject the drive..
http://support.microsoft.com/kb/311272
or you can also try DevEject
http://translate.google.com/translate?u=http://www.withopf.com/tools/deveject/&langpair=de%7Cen

My answer is not very much related but if you are willing to use Linux I would have a full software stack for exactly that. What it does is to hook into the Linux udev system and automatically formats USB pen drives that get connected to certain USB ports, then copies files to the drive, unmounts the drive and informs the user.
We used this to copy data to 500+ merchandise USB pen drives.

The best option I can find is this:
1) open a shell and run mountvol and locate GUID
\\?\Volume{1be3da43-6602-11e0-b9e6-f11e1c50f5b5}\
F:\
2) execute mountvol /p [GUID] in script
Dim eject
Set eject = WScript.CreateObject("WScript.Shell")
eject.Run "mountvol \\?\Volume{1be3da43-6602-11e0-b9e6-f11e1c50f5b5}\ /p"
Set eject = Nothing
The only problem with this method is that it needs administrator access to remove the drive letter. If called by a user it will unmount the drive, in this case leaving an F: phantom. It is safe to remove the USB drive, or you can eject the phantom to remove it.

Related

How to delete a device node of disk by program

I am developing the Windows program that removes specified disk from Windows system.
The action of "removing" does NOT mean unplugging HDD "physically".
It means that the program removes a disk from device node managed by OS.
"disk" what I'm saying is the disk that is managed by disk.sys(PhysicalDrive0, PhysicalDrive1..)
By the way, on usual Linux system, it can be done with a command like below.
echo 1 > /sys/block/sde/device/delete
I want to do same thing as above on Windows by program.
Once I have tried to do it by removing a disk in DeviceManager.
Actually this way could remove USB disk, but this way could not remove SATA disk without rebooting OS.
I want to do it without rebooting.
Is there any way to do it ?
I believe WMI might help you:
wmic logicaldisk delete (check with /? how to use it)
In order to know which ones you have now, launch the following first:
wmic logicaldisk list brief

how to display the volume name automatically on windows/linux

I am writing a USB driver to a product which is basically a USB pen drive. Upon connecting the product to the usb port, I want to see the drive name/volume name as "XXXX Corporation". I see that by default it always gets shown as "Removable Disk" in Windows explorer or my computer.
If I use any USB detail reader tools available in the net, then I can see the vendor id and manufacturer ids and other usb details. Renaming the drive from windows works but I want this to happen by default without asking the end user to rename. Having autorun.inf in root of the drive works only on WindowsXP.
How do I get this? Are there any descriptors which needs to be coded explicitly in my usb drivers stack? There are already API's to obtain the manufacturer/Product/Serial descriptors in my usb drivers stack.
Thanks in advance.
Chandra
The name shown in file explorers is the filesystem label, it's not a USB descriptor. The tool you are using to create the filesystem (whether FAT or NTFS) probably allows you to set the label. It can also be changed on an existing filesystem. The label can also be read on any operating system.
The label entry of the autorun.inf file should also be read by modern Windows. Some Linux file explorers (Gnome's Nautilus) might use it if it's there, but the cross-platform way is the filesystem label.

How to run .exe file from pen drive on insert?

I want to run a .exe file (or) any application from pen drive on insert in to pc. I dont want to use Autorun.inf file, as all anti virus software's blocks it. I have used portable application launcher also, that also using autorun only. so once again anti virus software blocks it. Is there any alternative option, such that .exe file from pen drive should start automatically on pen drive insert?
Anti-virus programs block autorun.inf on the solely purpose not to allow some .exe-s to start automatically on pen drive insert. So, basically, what you're asking is impossible.
I havent used Windows in a long time, but I am fairly sure there is a setting in Windows to enable/disable autorunning executeables on mounted drives. That and changing such setting in your antivirus application (or get a new, saner one) would be my best guess.
Good luck!

Network Shares of Removable Drives Disappear (Windows)

I have two removable drives. I can set the access rights so that they can be shared and used from another network computer, but when I start my main computer the next time and the drives are switched off, they seem to lose the share settings and I have to set the access rights again.
Is there a way to prevent this and to automatically share a complete drive so that it can be accessed across the network as soon as it is switched on the next time?
The long and the short of it is "No"... I had the same issue with a removable drive and there's not a way to do it. You CAN however, write a small service that will share the drive automatically on boot-up. Short of that, Windows just simply will not re-share a removable drive after reboot.
~md5sum~
I've had this problem, and after some research came to this registry solution that increases the number of shared removable drives that windows retains (why Windows is worried about not having enough space to remember unlimited numbers of shares is beyond me). http://forums.cnet.com/7726-6122_102-3230783.html#3230783

How to tell which disk Windows Used to Boot

I'm need to find a method to programmatically determine which disk drive Windows is using to boot. In other words, I need a way from Windows to determine which drive the BIOS is using to boot the whole system.
Does Windows expose an interface to discover this? With how big the Windows API is, I'm hoping there is something buried in there that might do the trick.
Terry
p.s. Just reading the first sectors of the hard disk isn't reveling anything. On my dev box I have two hard disks, and when I look at the contents of the first couple of sectors on either of the hard disks I have a standard boiler plate MBR.
Edit to clarify a few things.
The way I want to identify the device is with a string which will identify a physical disk drive (as opposed to a logical disk drive). Physical disk drives are of the form "\\.\PHYSICALDRIVEx" where x is a number. On the other hand, a logical drive is identified by a string of the form, "\\.\x" where x is a drive letter.
Edit to discuss a few of the ideas that were thrown out.
Knowing which logical volume Windows used to boot doesn't help me here. Here is the reason. Assume that C: is using a mirrored RAID setup. Now, that means we have at least two physical drives. Now, I get the mapping from Logical Drive to Physical Drive and I discover that there are two physical drives used by that volume. Which one did Windows use to boot? Of course, this is assuming that the physical drive Windows used to boot is the same physical drive that contains the MBR.
Go into Control Panel
System and Security
Administrative Tools
Launch the System Configuration tool
If you have multiple copies of Windows installed, the one you are booted with will be named such as:
Windows 7 (F:\Windows)
Windows 7 (C:\Windows) : Current OS, Default OS
Unless C: is not the drive that windows booted from.Parse the %SystemRoot% variable, it contains the location of the windows folder (i.e. c:\windows).
You can use WMI to figure this out. The Win32_BootConfiguration class will tell you both the logical drive and the physical device from which Windows boots. Specifically, the Caption property will tell you which device you're booting from.
For example, in powershell, just type gwmi Win32_BootConfiguration to get your answer.
That depends on your definition of which disk drive Windows used to boot. I can think of 3 different answers on a standard BIOS system (who knows what an EFI system does):
The drive that contains the active MBR
The active partition, with NTLDR (the system partition)
The partition with Windows on it (the boot partition)
2 and 3 should be easy to find - I'm not so sure about 1. Though you can raw disk read to find an MBR, that doesn't mean it's the BIOS boot device this time or even next time (you could have multiple disks with MBRs).
You really can't even be sure that the PC was started from a hard drive - it's perfectly possible to boot Windows from a floppy. In that case, both 1 and 2 would technically be a floppy disk, though 3 would remain C:\Windows.
You might need to be a bit more specific in your requirements or goals.
You type diskpart, list disk and check disks for boot.
Ex:
dispart
list disk
select disk 0
detail disk
The disk with Boot volume is disk with windows installed:
There is no boot.ini on a machine with just Vista installed.
How do you want to identify the drive/partition: by the windows drive letter it is mapped to (eg. c:\, d:) or by how its hardware signature (which bus, etc).
For the simple case check out GetSystemDirectory
Try HKEY_LOCAL_MACHINE\SYSTEM\Setup\SystemPartition
You can try use simple command line. bcdedit is what you need, just run cmd as administrator and type bcdedit or bcdedit \v, this doesn't work on XP, but hope it is not an issue.
Anyway for XP you can take a look into boot.ini file.
a simpler way
search downloads in the start menu and click on downloads in the search results to see where it will take you the drive will be highlighted in the explorer.
On Windows 10.
Open "Computer Management"
Look for "Storage" in list "left top side of page"
select "Disk Management"
On section of page showing the list of disks and the partitions find the disk that has the partition assigned as drive C:
On that disk containing C: partition
Use the right mouse button to select the Square section containing The Disk Number, Type of drive and size in GB . When menu opens select the Properties.
A window will open showing what drive hardware was used.

Resources