|
-
Oct 10th, 2007, 11:46 AM
#1
Thread Starter
Fanatic Member
[Resolved] Why is this code not working?
I'm trying to get a list of all the computers on my LAN. I have tried that huge long code that uses API calls and the such, but i want to stick with command prompt for this one (Simply because i may output it to a file later).
Well here's my code so far:
Code:
Private Sub CmdRefresh_Click()
On Error Resume Next
WorkTxt.Text = ""
WorkTxt.Text = GetCommandOutput("net view")
DoEvents
Dim Computers_() As String
Dim I_ As Integer
Computers_ = Split(WorkTxt.Text, vbCrLf)
For I_ = 0 To UBound(Computers_)
If Not Left(Computers_(I_), 2) = "\\" Then RemoveItem Computers_(), I_
Next I_
For I_ = 0 To UBound(Computers_)
ComputersList.AddItem Trim(Computers_(I_))
Next I_
End Sub
Private Sub RemoveItem(sArray() As String, ByVal Index As Integer)
Dim K As Integer
If Index < UBound(sArray) Then
For K = Index To UBound(sArray) - 1
sArray(K) = sArray(K + 1)
Next K
End If
ReDim Preserve sArray(UBound(sArray) - 1)
End Sub
The problem (Bolded) for some reason only outputs the first computer name (I have about 4 on my network). In the WorkTxt text box has the 4 computers listed. Also if i comment out the bolded lines it will print all the lines from command prompt into the list box (One line per line). But for some reason, it doesn't like it when using the Left function to check whether there's two "\\" on the front of the line.
Does anyone know why this is???
Last edited by Slyke; Oct 10th, 2007 at 11:32 PM.
-
Oct 10th, 2007, 12:22 PM
#2
Re: Why is this code not working?
Have you put a breakpoint at the If Not Left(Computers_(I_), 2) = "\\" Then RemoveItem Computers_(), I_ line to see what is in Computers_(I_)?
BTW just out of curiosity, why do you end your variables with underscores?
-
Oct 10th, 2007, 12:49 PM
#3
Re: Why is this code not working?
When you ignore errors with On Error Resume Next anything can happen!
Modifying the bounds of an array while it is being used in a For Loop will cause subscript out of bound errors.
The best approach is to loop through the array backwards.
For I_ = UBound(Computers_) To 0 Step -1
This way if an Index is removed the For Loop will have already processed that element.
-
Oct 10th, 2007, 01:13 PM
#4
Thread Starter
Fanatic Member
Re: Why is this code not working?
 Originally Posted by MartinLiss
Have you put a breakpoint at the If Not Left(Computers_(I_), 2) = "\\" Then RemoveItem Computers_(), I_ line to see what is in Computers_(I_)?
BTW just out of curiosity, why do you end your variables with underscores?
Hmmm... well i don't know, there's a few reasons. For one, it helps not to get mixed up with other variables that i might have, or parameters, keywords etc.
Yes, i have (Not a breakpoint, but a message box Msgbox Computers_(I_) Before the line and it prints each line out. It just doesn't like it after i use the Left function.
I'm speculating here, but i think there might be hidden Carriage Return or Line Feed characters somewhere which is causing the Left function to only read the first line.
@brucevde
Yes, i think you might be right too. I'm going to test it when i wake up in the arvo (lol). Since it prints everything out normally before the Left function, it might be correctly selecting the lines, but because the dimension changes, it's putting in the blank lines that are appearing.
-
Oct 10th, 2007, 11:32 PM
#5
Thread Starter
Fanatic Member
Re: Why is this code not working?
Yes thanks brucevde! Making the loop count backwards fixed it!
Code:
Private Sub CmdRefresh_Click()
On Error Resume Next
WorkTxt.Text = ""
WorkTxt.Text = GetCommandOutput("net view")
DoEvents
Dim Computers_() As String
Dim I_ As Integer
Computers_ = Split(WorkTxt.Text, vbCrLf)
For I_ = UBound(Computers_) To 0 Step -1
If Not Left(Computers_(I_), 2) = "\\" Then RemoveItem Computers_(), I_
Next I_
For I_ = 0 To UBound(Computers_)
ComputersList.AddItem Trim(Computers_(I_))
Next I_
End Sub
Private Sub RemoveItem(sArray() As String, ByVal Index As Integer)
Dim K As Integer
If Index < UBound(sArray) Then
For K = Index To UBound(sArray) - 1
sArray(K) = sArray(K + 1)
Next K
End If
ReDim Preserve sArray(UBound(sArray) - 1)
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|