Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - substitute for subst

Reply
 
Old 06-28-2008   #1 (permalink)
dlc


 
 

substitute for subst

is it possible to have a script to map a drive letter to a directory without
using subst?

My System SpecsSystem Spec
Old 06-28-2008   #2 (permalink)
Richard Mueller [MVP]


 
 

Re: substitute for subst


"dlc" <dlc@xxxxxx> wrote in message
news:F3D74709-39DE-45F1-8A46-851C1C47D47F@xxxxxx
Quote:

> is it possible to have a script to map a drive letter to a directory
> without
> using subst?
Yes. It's been a long time since I've seen subst used. It only works to map
local drives, but is actually faster as it does not use the network stack.
However, local drive mappings are rare, most are remote resources. Use the
MapNetworkDrive method of the wshNetwork object to map a share to a drive in
VBScript. For example:
=========
Option Explicit
Dim objNetwork, strShare, strDrive

strDrive = "K:"
strShare = "\\MyServer\MyShare"

Set objNetwork = CreateObject("Wscript.Network")
objNetwork.MapNetworkDrive strDrive, strShare
=======
The mapping will fail if the drive is in use, perhaps because the drive was
previously mapped as persistent. One way to handle this is to trap the
possible error, then attempt to remove the mapping (including making the
mapping no longer persistent), then try again to map the drive. For example:
=========
Option Explicit
Dim objNetwork, strShare, strDrive

strDrive = "K:"
strShare = "\\MyServer\MyShare"

Set objNetwork = CreateObject("Wscript.Network")

' Trap possible error if drive in use.
objNetwork.MapNetworkDrive strDrive, strShare
If (Err.Number <> 0) Then
' Restore normal error handling.
On Error GoTo 0
' Remove possible existing mapping. The first True
' removes the mapping even if it is in use. The second
' True makes the mapping no longer persistent.
objNetwork.RemoveNetworkDrive sDrive, True, True
' Try again to map the drive. If an error
' is raised, allow normal error handling so we
' see the error message.
objNetwork.MapNetworkDrive strDrive, strShare
End If
On Error GoTo 0

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 06-28-2008   #3 (permalink)
Richard Mueller [MVP]


 
 

Re: substitute for subst


"Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
message news:ecLGFTT2IHA.5832@xxxxxx
Quote:

>
> "dlc" <dlc@xxxxxx> wrote in message
> news:F3D74709-39DE-45F1-8A46-851C1C47D47F@xxxxxx
Quote:

>> is it possible to have a script to map a drive letter to a directory
>> without
>> using subst?
>
> Yes. It's been a long time since I've seen subst used. It only works to
> map local drives, but is actually faster as it does not use the network
> stack. However, local drive mappings are rare, most are remote resources.
> Use the MapNetworkDrive method of the wshNetwork object to map a share to
> a drive in VBScript. For example:
> =========
> Option Explicit
> Dim objNetwork, strShare, strDrive
>
> strDrive = "K:"
> strShare = "\\MyServer\MyShare"
>
> Set objNetwork = CreateObject("Wscript.Network")
> objNetwork.MapNetworkDrive strDrive, strShare
> =======
> The mapping will fail if the drive is in use, perhaps because the drive
> was previously mapped as persistent. One way to handle this is to trap the
> possible error, then attempt to remove the mapping (including making the
> mapping no longer persistent), then try again to map the drive. For
> example:
> =========
> Option Explicit
> Dim objNetwork, strShare, strDrive
>
> strDrive = "K:"
> strShare = "\\MyServer\MyShare"
>
> Set objNetwork = CreateObject("Wscript.Network")
>
> ' Trap possible error if drive in use.
> objNetwork.MapNetworkDrive strDrive, strShare
> If (Err.Number <> 0) Then
> ' Restore normal error handling.
> On Error GoTo 0
> ' Remove possible existing mapping. The first True
> ' removes the mapping even if it is in use. The second
> ' True makes the mapping no longer persistent.
> objNetwork.RemoveNetworkDrive sDrive, True, True
> ' Try again to map the drive. If an error
> ' is raised, allow normal error handling so we
> ' see the error message.
> objNetwork.MapNetworkDrive strDrive, strShare
> End If
> On Error GoTo 0
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
>
Also, here is an example VBScript logon script from my web site:

http://www.rlmueller.net/Logon3.htm

This includes a function to map drives using the logic I described above.
The example also has a function to check group membership so you can map
drives according to group membership, even if the groups are nested. It also
maps a printer according to the group the local computer is a member of.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 06-28-2008   #4 (permalink)
Tom Lavedas


 
 

Re: substitute for subst

On Jun 28, 11:36*am, "Richard Mueller [MVP]" <rlmueller-
nos...@xxxxxx> wrote:
Quote:

> "dlc" <d...@xxxxxx> wrote in message
>
> news:F3D74709-39DE-45F1-8A46-851C1C47D47F@xxxxxx
>
Quote:

> > is it possible to have a script to map a drive letter to a directory
> > without
> > using subst?
>
> Yes. It's been a long time since I've seen subst used. It only works to map
> local drives, but is actually faster as it does not use the network stack..
> However, local drive mappings are rare, most are remote resources. Use the
> MapNetworkDrive method of the wshNetwork object to map a share to a drivein
> VBScript. For example:
> =========
> Option Explicit
> Dim objNetwork, strShare, strDrive
>
> strDrive = "K:"
> strShare = "\\MyServer\MyShare"
>
> Set objNetwork = CreateObject("Wscript.Network")
> objNetwork.MapNetworkDrive strDrive, strShare
> =======
> The mapping will fail if the drive is in use, perhaps because the drive was
> previously mapped as persistent. One way to handle this is to trap the
> possible error, then attempt to remove the mapping (including making the
> mapping no longer persistent), then try again to map the drive. For example:
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab -http://www.rlmueller.net
> --
Well, I'll be ...

I woulda never ...

To tell the truth I had to see it for myself before I believed it -
but that's good to know. I was just blind to the thought of the local
machine as a mappable network resource - relative to itself. Duh!

The only drawback to this approach is that the folder must be shared
before it can be connected.

I reworked your approach to create the share and then do the mapping,
but it's hardly complete application. It would need to accept an
input argument path and select or request a drive letter and also
provide a means of disconnecting. All quite do-able, now that the
idea of using a network connection to a local resource in place of
SUBST is revealed.

Dim oNet, sShare, sDrive

sDrive = "K:"
sSharePath = "C:\Someplace\Testing"
sShareName = "Testing"

set oNet = CreateObject("Wscript.Network")
sNetShare = "\\" & oNet.ComputerName & "\" & sShareName
wsh.echo sNetShare

Const FILE_SHARE = 0
Const MAXIMUM_CONNECTIONS = 1

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")

Set oNewShare = objWMIService.Get("Win32_Share")

errRet = oNewShare.Create(sSharePath, sShareName, FILE_SHARE, 1,
sShareName)

if errRet = 0 then
oNet.MapNetworkDrive sDrive, sNetShare
else
wsh.echo "Failed"
end if

Tom Lavedas
===========
My System SpecsSystem Spec
Old 06-29-2008   #5 (permalink)
Pegasus \(MVP\)


 
 

Re: substitute for subst

"dlc" <dlc@xxxxxx> wrote in message
news:F3D74709-39DE-45F1-8A46-851C1C47D47F@xxxxxx
Quote:

> is it possible to have a script to map a drive letter to a directory
> without
> using subst?
As usual there is also a very concise batch file answer to your question:

@echo off
net use Q: "\\%ComputerName%\E$"

or perhaps a little more robust:

@echo off
net share | find /i "DriveE" > nul || net share DriveE=E:\
net use Q: "\\%ComputerName%\DriveE"


My System SpecsSystem Spec
Old 07-01-2008   #6 (permalink)
dlc


 
 

Re: substitute for subst



"Pegasus (MVP)" wrote:
Quote:

> "dlc" <dlc@xxxxxx> wrote in message
> news:F3D74709-39DE-45F1-8A46-851C1C47D47F@xxxxxx
Quote:

> > is it possible to have a script to map a drive letter to a directory
> > without
> > using subst?
>
> As usual there is also a very concise batch file answer to your question:
>
> @echo off
> net use Q: "\\%ComputerName%\E$"
>
> or perhaps a little more robust:
>
> @echo off
> net share | find /i "DriveE" > nul || net share DriveE=E:\
> net use Q: "\\%ComputerName%\DriveE"
>
>
> Thanks for all the replies. I will try them when I get to work. I wonder if normal users (non admin) are going to have the permissions to do these. I'll let you know.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Substitute Vista mail
Users can't run subst.exe or attrib.exe ?? Vista file management
Subst oddity with UAC Vista file management
Problem with SUBST on Vista Vista General
How do I recognize a subst drive? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46