![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | substitute for subst is it possible to have a script to map a drive letter to a directory without using subst? |
My System Specs![]() |
| | #2 (permalink) |
| | 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? 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 Specs![]() |
| | #3 (permalink) |
| | 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 > -- > > 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 Specs![]() |
| | #4 (permalink) |
| | 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 > -- 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 Specs![]() |
| | #5 (permalink) |
| | 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? @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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||