VBScript Working With Mounted Drives

by cammel8 in Circuits > Software

9566 Views, 6 Favorites, 0 Comments

VBScript Working With Mounted Drives

24.jpg
Image17.jpg
This instructable is by request. Many of you may recognise this from a previous instructable I had done wich was https://www.instructables.com/id/Intro_to_VB_Script_a_beginners_guide/. Well in that one techwiz24 asked me could you use this to disconnect a network drive, display a picture, and then ask to reconnect the network drive? and by network drive, I mean an external hdd.
when it reconnects, I also need it to name the external hdd, like HDD1e. I can get everything except the CMD commands for it. I think you can use diskpart, but am not sure,

Well I told him I would work on it and Create a new instructable if I figured it out. Well I figured it out. So here it is. If youhaven't read my previous instructable you can find it here. https://www.instructables.com/id/Intro_to_VB_Script_a_beginners_guide/ I would suggest reading it before reading this one if you havent read it yet as most of the stuff will be the same and I wont recover the basics.

And because lemonie complained in one of my other instructables that "In wanting to display wallpapers repeatedly you make this look rather bad. Using ALT-Print Screen would show the dialogue boxes better". I decided that I would not put up any pictures that actually had anything to do with the instructable since the instructable is not a visual thing anyway and the pictures would neither harm it nor help it so why take the time to make the screan captures just so someone can complain that I didnt screen cap them correctly.

So in this one every pick will be a background wallpaper that I like and screw if it looks bad or not!!!

Research

1968_Dodge_Charger.jpg
Image48.jpg
First thing we need to do is find out the GUID of the hard drive we want to remove. The GUID is a Globally Unique IDentifier. What this does is allows your computer to find a specific hard drive no matter what drive letter is assigned to it. An easy way to do this is to bring up a CMD window and type "MOUNTVOL"

This will bring up the help page for Mountvol. If you look to the bottum of the list it should show all available hard drives, their drive letters and thier paths.

By doing this on mine I garnered this information:
Creates, deletes, or lists a volume mount point.

MOUNTVOL [drive:]path VolumeName
MOUNTVOL [drive:]path /D
MOUNTVOL [drive:]path /L

path Specifies the existing NTFS directory where the mount
point will reside.
VolumeName Specifies the volume name that is the target of the mount
point.
/D Removes the volume mount point from the specified directory.
/L Lists the mounted volume name for the specified directory.

Possible values for VolumeName along with current mount points are:

\\?\Volume{6ad2db35-4ab2-11de-964e-806d6172696f}\
C:\

\\?\Volume{6ad2db33-4ab2-11de-964e-806d6172696f}\
D:\

\\?\Volume{6ad2db34-4ab2-11de-964e-806d6172696f}\
E:\

\\?\Volume{b76a1f58-662f-11de-8a61-001111cb3c76}\
F:\

\\?\Volume{6ad2db32-4ab2-11de-964e-806d6172696f}\
A:\

\\?\Volume{c0ea045c-56fe-11de-8a5a-001111cb3c76}\
G:\
___________________________________________________________________________

Now if you look at the bottum of the list you will see a bunch of gobbledygook that starts with \\?\volume and ends with a drive letter. This is what we need!

Ok so lets break down what we have here :
We have volume represented by \\?\volume then we have a curly bracket then a bunch of numbers, letters and dashes. The numbers letters and dashes are actually the GUID for the drive in question then we have another curly bracket then a back slash and the drive letter. So that should be easy enough to follow.
So in my computer I have
\\?\Volume{6ad2db35-4ab2-11de-964e-806d5172696f}\
C:\
this is my main hard drive for the computer
\\?\Volume{6ad2db33-4ab2-11de-964e-806d5172696f}\
D:\
Which is a dvd writer
\\?\Volume{6ad2db34-4ab2-11de-964e-806d5172696f}\
E:\
Another dvd writer
\\?\Volume{b76a1f58-662f-11de-8a61-001114cb3c76}\
F:\
A fake dvd so I can mount ISO files created my a Daemon program I have
\\?\Volume{6ad2db32-4ab2-11de-954e-806d6172696f}\
A:\
A 3.5 floppy drive.... Yes I know I know but sadly I still use them once in a great while.
\\?\Volume{c0ea045c-56fe-11de-8a5a-001111cb3c76}\
G:\
This one here is the drive in question that I want to remove

In this instance it is actually a 128meg jump drive I used for testing. This way if I buggered it up I didnt loose a hard drive or something.

Using Mountvol

5475475.jpg
random.098.jpg
Ok now that we have the GUID we can use it to mount and unmount the drive in question

Now before we go and do all the work of writing a script we want to make sure it works and is the correct drive. g drive is the drive I am working with so mine will say g:\ however you need to put in the drive letter of the drive you are woking with. same witht he GUID, as it is unique to each drive and yours WONT be the same as mine.

We will basically just use a delet switch "/d" to delete the mount point then use mount comand to remount it using the guid as a reference to the unmounted disk.

So bring up a cmd prompt by going to run and typing cmd

at the prompt type in:
mountvol g:\ /d

now when we type mountvol in the prompt all is the same except now it says:
\\?\Volume{c0ea045c-56fe-11de-8a5a-001111cb3c76}\
*** NO MOUNT POINTS ***

instead of

\\?\Volume{c0ea045c-56fe-11de-8a5a-001111cb3c76}\
G:\

This is good it means we removed the one we wanted to.

Now we need to remount it

Type this at the comand prompt:
mountvol g: \\?\Volume{c0ea045c-56fe-11de-8a5a-001111cb3c76}\

and then type mountvol one more time and everything should be back to the way it was

The context to reenabling the drive is as follows:
The word mountvol followed by a space then the drive letter and a colon folowed by a space then a backslash backslash question mark backslash then the word volume then a open curly bracket then the GUID then a close curly bracket then a backslash

Working With Labels

a6ef93ea2c29abceded3ef8dd78357f1_full.jpg
Strange_Love_by_cherry65.jpg
Now part of what techwiz24 asked me was when it reconnects, I also need it to name the external hdd, like HDD1e.

To do that we simply use the label command its pretty straight forward

Just bring up a command window same as last time and type:

g:

This will change your directory to g:>

Then type vol and take not of the volume label

Then just type label and whatever you want it to be called so if I wanted to name my hard drive bobby I would type:
label bobby

Then just type vol again and it will show the new name of the volume is bobby

Now that only works if you are working in the directory of the volume you want to change however when we make our script we dont want to change directory then change volume so we would just type:
label g: bobby

Told you it was petty simple.

Adding What We Learned Into the Script

CG Artwork Wallpapers Collection-4 15.jpg
Windows_-_Test_the_system.jpg
Ok so now we just make our script the way we did before but instead of using ipconfig comands we will now be using mountvol comands.

Here is the code for disableing the drive:

set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"
wscript.sleep 200
shellobj.sendkeys "mountvol g:\ /d{enter}"

_

You should understand all of this so far from our little tests

Here is the code to open a picture like he wanted and close the first command prompt window:

wscript.sleep 2000
shellobj.sendkeys "c:\3.jpg{enter}exit{enter}"

_

As you can see you can add multiple commands to one line as long as the window is still open. It will wait till the last command has been executed before starting the next one.

Here is the code for the pop up boxs so your script stays running until you reenable the drive:
Dim MyVar
myvar=2
while myvar=2
MyVar = MsgBox ("Turn drive back on", 65, "Turn drive back on")
wend

msgbox("We will now Turn hard drive back on!!!")

_

Note that by making the Myvar variable = to 2 wich is the value of the cancel button on the message box we will run a continuous loop until the ok button is pressed. This forces us to click yes to end the script. Only after yes is pushed does the myval value change to 1 and allow the loop to be broken therfore continuing the script and throwing up the messagebox saying the drive will then be reenabled.

Now if you remember from before it wont do the next comand until the last command has been finished so until the ok button is pressed on the messagebox no action will be taken.

Yes we could just use the messagebox to reenable the drive but then if you dont want it enabled yet but accidently click ok there is no safeguard. With the yes/no box you have no choice but to click yes.

We could get rid of the extra messagebox but I like having a confirmation that the action is being taken.

Now we get to where the remounting, labeling and cleaning up is done. You would think well this is just straight forward but its not. If you notice every time we want to place an enter or tab key into our sendkeys line we just type {ENTER}. The reason for this is because it sees the { and } as special characters. Therefore if we use the cutrly brackets like they are printed in the guid {c0ea045c-56fe-11de-8a5a-001111cb3c76} the sendkeys command would esentually press the c0ea045c-56fe-11de-8a5a-001111cb3c76 button. Since there is no such button our script would crash and then no remounted drive for us.

So how do we get around that? Well ironically the answer is in the brackets themself. Since anything thtat is within the open and close curly brackets will esentially be sent as a pressed key we just put the bracket we want inside the open and close curly brackets. So an open would be {{} and a close would be {}}

So now the end of our code would look like this:
shellobj.run "cmd"
wscript.sleep 200
shellobj.sendkeys "mountvol g: \\?\Volume{{}c0ea045c-56fe-11de-8a5a-001111cb3c76{}}\{enter}label g: HDD1e{enter}taskkill /F /IM rundll32.exe{enter}exit{enter}"

The Completed Script

Hackers_by_jubei123.jpg
ws_Windows_Invasion_1024x768.jpg
Here is the completed script without interuptions that you can just slide right into your script and be done with it. You must redirect the line that references the picture to a picture you want in a directory you want but remember dont use a directory on the volume you are removing or it will crash out becasue it wont find it in the directory. Also remember that you have to change the drive letters and GUID to your actual ones as these reflect mine and could possibly bugger up your system. Look fo the Rem lines before the line you need to change and you should be fine:

set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"
wscript.sleep 200
rem change the following line to reflect the drive letter of your drive
shellobj.sendkeys "mountvol g:\ /d{enter}"
wscript.sleep 2000
rem change the folowing line to reflect the actual name and directory of the picture your using
shellobj.sendkeys "c:\3.jpg{enter}exit{enter}"

Dim MyVar
myvar=2
while myvar=2
MyVar = MsgBox ("Turn drive back on", 65, "Turn drive back on")
wend

msgbox("We will now Turn hard drive back on!!!")
shellobj.run "cmd"
wscript.sleep 200
rem change the GUID to your actual GUID and the drive letters (both of them)to the actual drive
rem letter and the label to any label you want the drive to be labeled
shellobj.sendkeys "mountvol g: \\?\Volume{{}c0ea045c-56fe-11de-8a5a-001111cb3c76{}}\{enter}label g: HDD1e{enter}taskkill /F /IM rundll32.exe{enter}exit{enter}"

No Steps Really Just a Comment

00008.jpg
Well I hope that helped ya.... If anything isnt explained well enough let me know and i will try to explain better. And I hope everyone liked the wallpapers. No i didnt make them.... they are all ones I have found on the net.

NOTE:: If anyone has any complaints about my instructable please forward them to getalife@quityourwhining.com. I mean seriously, do you really think I give a dang what you think? If you dont want to learn something then dont, If you do then do, but don't be a jerk and complain because the non esential pictures aren't formated correctly or whatever cause frankly I DONT CARE!!!