Contents for Ezine 74 Map 1st Available Drive Letter
- This Week’s Secret
- This Week’s Mission – To Map to the 1st Available Drive Letter
- Example – To Map to the 1st Available Drive Letter
- Summary – Map 1st Available Drive Letter
Can you remember back to when you were a 10 year old? I bet that not all your classmates could catch a ball. I bet again, that not all your classmates were good readers. If we look at today’s typical 10 year olds, the story would be much the same. However, such is the simplicity yet power of computers; I bet that all normal 10 year olds can use a computer. Now here is where I make my quantum leap. You may not be brilliant at algebra, calculus or C++ programming, but just as every normal 10 year old can use a computer, every one reading this ezine can be a VBScript programmer. By a VBScript programmer, I mean that you can write your own simple VBScript programs that will do proper computer network ‘stuff’. How will you learn this ‘stuff’? The answer is by copying and pasting other people’s code and then making your own modifications.
Unfortunately, I spend less than 20% of my time with VBScript. Yet every time I go back to VBScript, I say to myself, I must do this more often. I confessed a few weeks ago that I too copy and adapt other people’s scripts, but this week once again, I have pretty much designed my own script, naturally, I employ tried and tested VBScript objects and methods. My point is that VBScript has all the building blocks, it just up to you and to me to have the vision, the plan and the time to ‘play’ with the code.
Guy Recommends: The Free IP Address Tracker (IPAT)
Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets. IPAT cracks this problem of allocating IP addresses in networks in two ways:
For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges.
For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker
Have you ever had the problem where you try to map a network drive, but unfortunately the drive letter is already in use? Well my scripts seeks to test for mapped drives, which are already in use, and if necessary, to pick another letter. The scenario is that you want to map a network drive, however, you do not really mind which letter the script chooses. Therefore, you provide a list of alternative letters and the script maps the first free drive letter.
Scripts constantly remind me that they just automate what we humans can do manually. Where scripts are at their best, is when there is a simple but repetitive sequence of events. The trick is to introduce a loop. The trap is not to put essential bits inside the loop, or else leave vital commands outside the loop. What makes this week’s script complex, is that I have two loops one inside the other.
The question my script keeps asking is ‘Does this particular drive letter exist?’ If the answer is false, then the drive letter does not exist, so the script maps that particular letter. Job done. However if that particular letter is already in use, then the loop must provide the next letter in the sequence and repeat my DriveExists test.
Instructions for mapping to the first available drive letter
- Copy and paste the script below into notepad.
- Create a network share. Edit Line 14
- Save the file with a .vbs extension e.g. IfExists .vbs.
- Double click the script, read the message box, check Explorer
‘ IfExists.vbs Windows Logon Script
‘ VBScript to map 1st available drive letter
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 3.6 – May 2005
Dim strDriveLetter, strRemotePath
Dim objNetwork, objShell
Dim CheckDrive, DriveExists, intDrive
Dim strAlpha, strExtract, intAlpha, intCount
‘ The section sets the variables
‘ N.B. Change strRemotePath
strRemotePath = "\\alan\usersHome"
strDriveLetter = "L:"
strAlpha = "LMNOPQ"
intAlpha = 0
intCount = 0
‘ This sections creates two objects:
‘ objShell and objNetwork and then counts the drives
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set CheckDrive = objNetwork.EnumNetworkDrives()
‘ This section operates the For … Next loop
‘ See how it compares the enumerated drive letters
‘ With strDriveLetter
On Error Resume Next
DriveExists = False
For intCount = 1 To 5
DriveExists = False
‘ CheckDrive compares each Enumerated network drive
‘ with the proposed drive letter held by strDriveLetter
For intDrive = 0 To CheckDrive.Count – 1 Step 2
If CheckDrive.Item(intDrive) = strDriveLetter _
Then DriveExists = True
intAlpha = intAlpha + 1
‘ Logic section if strDriveLetter does not = DriveExist
‘ Then go ahead and map the drive
Wscript.Echo strDriveLetter & " exists " & DriveExists
If DriveExists = False Then objNetwork.MapNetworkDrive _
If DriveExists = False Then objShell.run ("Explorer" &" "_
& strDriveLetter & "\" )
If DriveExists = False Then WScript.Quit(0)
‘ If the DriveExists, then it is necessary to
‘ reset true –> false for next test loop
If DriveExists = True Then DrivExists = False
‘ Appends a colon to drive letter
strDriveLetter = Mid(strAlpha, intAlpha,1) & ":"
WScript.Echo "Out of drive letters. Last letter " & strDriveLetter
‘ Guy’s If Exists VBScript ends here
Note 1: Examine and trace the outer loop. The idea is to focus on strDriveLetter. Ask does this letter = any of the existing mapped network drives? If yes, then loop and get another letter from strAlpha. If no then bingo, map this drive because it is free and there is no danger of a conflict.
Note 2: The inner loop is where the script compares strDriveLetter with each drive discovered by the EnumNetworkDrives function.
Note 3: You may prefer to choose a different selection of drive letters for strAlpha. The list does not have to be in alphabet order.
Note 4: See the importance of EnumNetworkDrives, and note the plural spelling of this method.
Challenges : If you take up my theme of developing your own scripts; experiment with different variable names, amend my WScript.Echo messages, add error correcting code, choose different letters to map.
Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.
It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.
It is annoying if a map network drive fails because the drive has already been mapped. This script loops through a sequence of letters until it finds a free drive letter. Note how it uses not only MapNetworkDrive, but also EnumNetworkDrives.
See more about logon scripts