I want to be able to connect to a (wifi) network hard drive from my laptop, but only occasionally. If I use the "Map a network drive" command in WinXP explorer, I have to put in the drive's IP address and name, then the router name and its password. Too much to remember!
I'm looking for a way of scripting this activity (in any language), something like:
map Z: \\10.0.1.1\DRIVENAME "ROUTERNAME\PW"
I don't particularly care what language the script is written in. BTW, I'm aware of the DOS 'subst' command, but I don't think I can use that in this case because of the password protection.
This question is related to
windows
language-agnostic
scripting
Does this not work (assuming "ROUTERNAME" is the user name the router expects)?
net use Z: "\\10.0.1.1\DRIVENAME" /user:"ROUTERNAME" "PW"
Alternatively, you can use use a small VBScript:
Option Explicit
Dim u, p, s, l
Dim Network: Set Network= CreateObject("WScript.Network")
l = "Z:"
s = "\\10.0.1.1\DRIVENAME"
u = "ROUTERNAME"
p = "PW"
Network.MapNetworkDrive l, s, False, u, p
Why not map the network drive but deselect "Reconnect at logon"? The drive will only connect when you try to access it. Note that some applications will fail if they point to it, but if you're accessing files directly through Windows Explorer this works great.
Tomalak's answer worked great for me (+1)
I only needed to make alter it slightly for my purposes, and I didn't need a password - it's for corporate domain:
Option Explicit
Dim l: l = "Z:"
Dim s: s = "\\10.10.10.1\share"
Dim Network: Set Network = CreateObject("WScript.Network")
Dim CheckDrive: Set CheckDrive = Network.EnumNetworkDrives()
Dim DriveExists: DriveExists = False
Dim i
For i = 0 to CheckDrive.Count - 1
If CheckDrive.Item(i) = l Then
DriveExists = True
End If
Next
If DriveExists = False Then
Network.MapNetworkDrive l, s, False
Else
MsgBox l + " Drive already mapped"
End If
Or if you want to disconnect the drive:
For i = 0 to CheckDrive.Count - 1
If CheckDrive.Item(i) = l Then
WshNetwork.RemoveNetworkDrive CheckDrive.Item(i)
End If
Next
Try the net use command
Here a JScript variant of JohnB's answer
// Below the MSDN page for MapNetworkDrive Method with link and in case if Microsoft breaks it like every now and then the path to the documentation of now.
// https://msdn.microsoft.com/en-us/library/8kst88h6(v=vs.84).aspx
// MSDN Library -> Web Development -> Scripting -> JScript and VBScript -> Windows Scripting -> Windows Script Host -> Reference (Windows Script Host) -> Methods (Windows Script Host) -> MapNetworkDrive Method
var WshNetwork = WScript.CreateObject('WScript.Network');
function localNameInUse(localName) {
var driveIterator = WshNetwork.EnumNetworkDrives();
for (var i=0, l=driveIterator.length; i < l; i += 2) {
if (driveIterator.Item(i) == localName) {
return true;
}
}
return false;
}
function mount(localName, remoteName) {
if (localNameInUse(localName)) {
WScript.Echo('"' + localName + '" drive letter already in use.');
} else {
WshNetwork.MapNetworkDrive(localName, remoteName);
}
}
function unmount(localName) {
if (localNameInUse(localName)) {
WshNetwork.RemoveNetworkDrive(localName);
}
}
Source: Stackoverflow.com