Script to display a network drive

I want to be able to connect to a (Wi-Fi) network hard drive from my laptop, but only occasionally. If I use the "Network Drive Card" command in WinXP Explorer, I must specify the IP address and drive name, and then the router name and password. Too much to remember!

I am looking for a way to write this operation (in any language), for example:

map Z: \\10.0.1.1\DRIVENAME "ROUTERNAME\PW" 

I don't really like what language the script is written in. By the way, I know the DOS command under "subst", but I don’t think I can use this in this case because of password protection.

+6
language-agnostic windows scripting
source share
6 answers

use net use command:

 net use Z: \\10.0.1.1\DRIVENAME 

Edit 1: In addition, I believe that the password should just be added:

 net use Z: \\10.0.1.1\DRIVENAME PASSWORD 

You can learn more about this command and its arguments with:

 net use ? 

Edit 2: As Tomalak mentioned in the comments, you can later display it using

 net use Z: \delete 
+25
source share

Does this work (assuming "ROUTERNAME" is the username that the router expects)?

  net use Z: "\\ 10.0.1.1 \ DRIVENAME" / user: "ROUTERNAME" "PW"

Alternatively, you can use a little 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 
+12
source share

Tomalak's answer is perfect for me (+1)

I needed to change it a little for my purposes, and I do not need a password - it is for a 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 
+4
source share

Why not display a network drive, but clear the "Restore at login" checkbox? The drive will only connect when trying to access it. Please note that some applications will fail if they point to it, but if you access files directly through Windows Explorer, this works fine.

+3
source share

Try the net use command

+2
source share

Here's a JScript JohnB answer option

 // 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); } } 
+1
source share

All Articles