Remove Network Drive's

Hello all

Im curetly at a quandry as the apart of a VB Script i have writen keeps coming up with an error

VBScript: well the part where the error is.

'Remove Drives from user
      Set WshNetwork = CreateObject("Wscript.network")
      Set Drives = WshNetwork.EnumNetworkDrives
      For i = 0 to Drives.Count - 1 Step 2
      connection = Drives.Item(i+1)
WshNetwork.RemoveNetworkDrive Drives.Item(i), true, true
Next

ERROR:

Windows Script Host
Script:    \\Server\NETLOGON\Logoff.vbs
Line:      24
Char:     1
Error:    This network connetion does not exist

Code:    800708CA
Source: WWHSNetwork.RemoveNetworkDrive

All I am tring to do is Disconect all the networks drives when a member of my organisation logsoff as we have to provide a roming capability for the higher managment and coperate members the same accessability to move freely about the place
LVL 1
Nicky RavenTechnical Services AnalystAsked:
Who is Participating?
 
TimCotteeConnect With a Mentor Head of Software ServicesCommented:
Hi HizShadow,

Do it backwards, when you have removed half the drives it will error every time.

     For i = Drives.Count - 1 To 0 Step -2
     connection = Drives.Item(i)
WshNetwork.RemoveNetworkDrive Drives.Item(i-1), true, true
Next

Tim Cottee
0
 
sk33v3Commented:
here is another Idea

Use a do loop and

Do until lngx>drivesCount
     WshNetwork.RemoveNetworkDrive 1, true, true
     lngx=lngx+1
loop

This should work... Although I am not on my development machine to test this out.
0
 
sk33v3Commented:
Oh sorry one more thing. Try to stay away from For loops as much as possible they execute very slowly.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
fds_fatboyCommented:
sk33v3:

>> Try to stay away from For loops as much as possible they execute very slowly.

Do you have documentation or benchmarks for this?
0
 
sk33v3Commented:
well here is a link to the test program I am using for this.
1000 objects were added
the do loop was consistently executing in 625 milliseconds
the for loop was executing in about 687 milliseconds.

62 milliseconds may not be much but if you start looking at some of the larger things that use lots of objects or where milliseconds matter then its a big deal.

http://www.grbackup.com/EE/for_VS_Do.zip
0
 
gecko_au2003Commented:
Not sure if this is of any help but is there a resume or resume next when it comes to vbscript ? Which is basically apart of error handling ?

Also just wanted to say thanks to sk33v3 for mentioning and showing that the do loop is faster then the for loop !
0
 
sk33v3Commented:
But what was disturbing is that Microsoft Documentation states that there is no significant difference.

um have you tried putting in on error resume next.
I believe that does work in VBScript. However if he just uses that then only half of the mapped drives will be removed. See his problems stems from the fact that he doesn't account for the removed drives when he removes them. Which is why TimCottee is probably the best answer to this problem.
0
 
fds_fatboyCommented:
Sk33v3:

Hmm...

Your code does not have a standard a for next loop but is a For Each loop. Anyway 1000 iterations is not enough to work out a time. It is very difficult to get a true benchmark like this as too many external factors (other windows processes, network, mouseevents etc) will always  . Also GetTickCount only works to the resolution of your system, It does not give a true number of milliseconds.

Anyway I've used your code plus  the following code (a normal For Next Loop)  on my (home machine) and found this:

1000 iterations: 10 runs each
Do:          156,141,156,156,156,157,157,156,156,156
For Each: 141,140,141,141,141,140,141,141,140,141
For:         141,141,141,141,156,156,157,140,156,157

This was more interesting - 10,000 iterations. 10 runs each
Do:          16218,16187,16171,16156
For Each:  
For:        



Code Follows:
Private Sub cmdForNext_Click()
    Dim TmpTest As Testing
    Dim LngX As Long
    Dim LngCount As Long
    Dim LngStart As Long
    Dim LngEnd As Long
   
    LngStart = GetTickCount
    LngCount = TestCollection.Count
   
    For LngX = 1 To LngCount
        Debug.Print TestCollection(LngX).Value
    Next
    LngEnd = GetTickCount
    Me.cmdForNext.Caption = "Test For Next" & vbCrLf & LngEnd - LngStart
    MsgBox LngEnd - LngStart
End Sub
0
 
fds_fatboyCommented:
Sorry Accidentally submitted:

1000 iterations: 10 runs each
Do:          156,141,156,156,156,157,157,156,156,156
For Each: 141,140,141,141,141,140,141,141,140,141
For:         141,141,141,141,156,156,157,140,156,157#

Of course If I run with the immediate window open, You can add another 120 or so milliseconds on tho these values

This was more interesting - 10,000 iterations. 10 runs each
Do:          16218,16187,16171,16156,16219,16156,16390,16188,16172,16188
For Each: 1985,1985,1969,2078,1984,2000,1969,1985,1969,1969
For:         16203,16125,16125,16125,16141,16157,16141,16281,16156

If you redo the last test but with the debug.print lines commented out therefore we are getting a better look at the loops: here are my results:
10,000 iterations 1 run
Do:          0 (Too fast to show up)
For Each: 16
For:         0 (Too fast to show up)

 
100,000 iterations 1 run
Do:          16
For Each: 94
For:         0 (Too fast to show up)

1,000,000 iterations, 10 runs
Do:          125,125,141,125,125,125,125,125,125,125
For Each:  1015, Crashed...
For:         15,15,15,15,16,32,16,16,15,31

Then it was 3am and my wife wondered why I was still up at the PC. So goodnight.
0
 
sk33v3Commented:
Hmm, just out of curiousity what kind of CPU do you have?
0
 
wraith821Connect With a Mentor Commented:
works beautifully.

Dim fso
Set fso = CreateObject("scripting.filesystemobject")

Dim d, dc, s


    Set dc = fso.Drives
    For Each d In dc
        DoEvents
        If d.drivetype = 3 Then Shell "net use " & d.driveletter & ": /delete"
    Next
0
 
wraith821Commented:
dont worry about the s variable, i forgot to take it out.
0
 
fds_fatboyCommented:
>> Hmm, just out of curiousity what kind of CPU do you have?

At home
AMD XP2000+, 1GB of PC2100 ram running Windows 2K SP4.

I learnt a long time ago that  a For-Next loop is the most efficient simple loop with a counter. Do should be more efficient without a counter.
For Next and Do are very similar Loops and very close in efficiency.

For Each cannot be compared on a like for like basis because it is doing something completely different to the other two. Personally I was surprised quite how quickly the For Each went on the 10K iteration runs.

Try this one which is a straight test between loops with counters:

'_____________________
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Const ITERATIONS As Long = 1000000000

Public Sub Main()
    Dim t As Long
    Dim i As Long
    Dim t1

    t = GetTickCount

    For i = 1 To ITERATIONS
    Next

    t1 = GetTickCount - t
    Debug.Print "For-Next", i, t1,

    t = GetTickCount

    i = 0 ' to get a true comparison this should go into the timed part
    Do Until i = ITERATIONS
        i = i + 1
    Loop

    t1 = GetTickCount - t
    Debug.Print "Do-Until", i, t1
End Sub
'_____________________

Output (using 2.8Ghz P4 with 1GB DDR Ram - Don't know the speed:
For-Next       100001        0            Do-Until       100000        16
For-Next       100001        0            Do-Until       100000        0
For-Next       100001        0            Do-Until       100000        16
For-Next       100001        0            Do-Until       100000        15
For-Next       100001        0            Do-Until       100000        0
For-Next       1000001       16           Do-Until       1000000       62
For-Next       1000001       15           Do-Until       1000000       63
For-Next       1000001       15           Do-Until       1000000       63
For-Next       1000001       16           Do-Until       1000000       62
For-Next       1000001       15           Do-Until       1000000       63
For-Next       10000001      125          Do-Until       10000000      797
For-Next       10000001      125          Do-Until       10000000      797
For-Next       10000001      79           Do-Until       10000000      734
For-Next       10000001      125          Do-Until       10000000      797
For-Next       10000001      109          Do-Until       10000000      797
For-Next       1000000001    11078        Do-Until       1000000000    79297
0
 
Nicky RavenTechnical Services AnalystAuthor Commented:
TimCottee your a star thanx alot its works fine and it removes all  maped network drives

wraith821 yours works as well but its not the one im going to use but ill give you some points for your imput

thankz alot all of ya
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.