?
Solved

Remove Network Drive's

Posted on 2005-04-13
14
Medium Priority
?
1,241 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:Nicky Raven
  • 5
  • 4
  • 2
  • +3
14 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 800 total points
ID: 13773194
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
 
LVL 9

Expert Comment

by:sk33v3
ID: 13773438
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
 
LVL 9

Expert Comment

by:sk33v3
ID: 13773451
Oh sorry one more thing. Try to stay away from For loops as much as possible they execute very slowly.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Expert Comment

by:fds_fatboy
ID: 13774695
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
 
LVL 9

Expert Comment

by:sk33v3
ID: 13776017
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
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13776685
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
 
LVL 9

Expert Comment

by:sk33v3
ID: 13776755
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
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 13778181
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
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 13778381
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
 
LVL 9

Expert Comment

by:sk33v3
ID: 13778615
Hmm, just out of curiousity what kind of CPU do you have?
0
 
LVL 8

Assisted Solution

by:wraith821
wraith821 earned 200 total points
ID: 13778816
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
 
LVL 8

Expert Comment

by:wraith821
ID: 13778819
dont worry about the s variable, i forgot to take it out.
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 13781077
>> 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
 

Author Comment

by:Nicky Raven
ID: 13788827
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question