Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

batch file won't disconnect network drive consistently

Status
Not open for further replies.

ttroxell

Technical User
Nov 21, 2002
24
US
Hi,

I've written the following login batch file to check to see if the drive exists, if found unmaps, then remaps the drive to the letter I've selected.

The issue is sometimes when the drive is already mapped, the script won't disconnect from the drive, so the drive won't map later in the script. On consective runs of the script will disconnect the drive. This is a shortened version of the script, but I have 6 drives I want mapped.

Any help would be greatly appreciated.

@echo off

if exist f: goto drivefound2 else
goto end
:drivefound2
net use f: /del
:end

pause

if exist l: goto drivefound3
goto end
:drivefound3
net use l: /del
:end

pause

if exist r: goto drivefound4
goto end
:drivefound4
net use r: /del
:end

pause

net use f: \\drivea
net use l: \\driveb
net use r: \\drivec

:END
echo DONE!
 
I think it is a little confusing to have goto statements pointing to label end, as you have four of them.

Be that as it may, what happens when you remove @echo off and run the .bat file?
 
I thought I would have to end after each if statement. As I want the process to continue through each drive to the end. Are you saying that I don't need it?

The bat file runs, but it won't always unmap a drive. Say I have a drive mapped to another users c drive as f. When I login and run the script, it won't always unmap the drive. If I logout and login again, it will remove it. But it's not consistent, it may work one time and then not the next.

Also, say I have another drive mapped to f, then I manually unmap the drive and map another. When I logout it should unmap the drive, and then remap it as the script requires.

The script runs fine, it reports each command completes successfully. To test it, I placed pauses after each if statement and in between each netuse statement. No errors were reported.

I've used this structure before in an NT environment and did not have any problems. I'm trying to use it in an XP environment, and I'm stumpped to why it won't work.
 
My concern is that the @echo off is hiding error messages in the instances where it does not un-map a drive letter. For example, that there are open files on the mapped drive letter.

This is one instance where WSH scripting has a decided edge because it allows one to un-map the drive even in the case of an error state; e.g.:

------------- force a mapped drive removal, renewal
Option Explicit
Dim objNetwork, objShell
Dim strDriveLetter1, strDriveLetter2, strDriveLetter3
strDriveLetter1 = "F:"
strDriveLetter2 = "L:"
strDriveLetter3 = "R:"
Dim strRemotePath1, strRemotePath2, strRemotePath3
strRemotePath1 = "\\drivea"
strRemotePath2 = "\\driveb"
strRemotePath3 = "\\drivec"

' Create logical variables to force removal
Dim bForce, bUpdateProfile
bForce = "True"
bUpdateProfile = "True"

Set objNetwork = CreateObject("WScript.Network")

' Removes strDriveLetter, with bForce, pUpdate Profile
On Error Resume Next

objNetwork.RemoveNetworkDrive strDriveLetter1, _
bforce, bUpdateProfile
objNetwork.RemoveNetworkDrive strDriveLetter2 _
bforce, bUpdateProfile
objNetwork.RemoveNetworkDrive strDriveLetter3, _
bforce, bUpdateProfile

' Now do the new mappings
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2
objNetwork.MapNetworkDrive strDriveLetter3, strRemotePath3

' Extra code just to add a message box
WScript.Echo "Map drives " & strDriveLetter1 & " & " & strDriveLetter2

WScript.Quit

 
Issue has been resolved. During login sequence other processess causing the drives not to map and unmap properly.

Set GP "Run LogonScript Sync" policy, prevents the shell to be started before all logon scripts are finished.

Thanks for all the input.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top