0

I have the below program and it's working fine, the function of the program that it takes the IP address along with host name from the array and then pass it down by sub-string the IP/host name, and then it creates .ZIP file "using 3rd party library" on the remote machine and get the file transfer to a central server. My requirements which am stuck with:

  1. How to make my program to pick up my computer list from .txt file instead of embedding it in the program, so if there's a new machine IP need to be added I just need to add it in .txt file

  2. How I can make my process runs in parallel, since my program will run IP after IP in serial which takes longer time than executing all at once.

Code:

Imports System.IO
Module Module1

    Dim RemoteComputer As New List(Of String)
    Const FILENAME As String = "c:\temp\test.txt"

    Sub Main()
         Try
                Dim reader As New StreamReader(FILENAME)
                While reader.EndOfStream = False
                    RemoteComputer.Add(reader.ReadLine)
                End While
            For Each ComputerName In RemoteComputer
                Dim IPAddress As String = ComputerName.ToString.Substring(0, 13).ToString()
                Dim CName As String = ComputerName.ToString.Substring(14, 6).ToString()
                Dim ZipToCreate As String = "\" & IPAddress & "\C$\temp\Test-" & CName & ".zip"
                If My.Computer.Network.Ping(Trim(IPAddress.ToString())) Then
                    Using zip As ZipFile = New ZipFile
                        Dim filenames As String() = System.IO.Directory.GetFiles("\" & IPAddress & "\C$\Sample\")
                        For Each filename In filenames zip.AddFile(filename, "") 
                        Next
                        zip.Save(ZipToCreate)
                        File.Copy("\" & IPAddress & "\C$\temp\Test-" & CName & ".zip", "\destSeverIP\C$\Temp\Test-" & CName & ".zip", True)
                        Console.WriteLine("File copied successfully.....")
                    End Using
                Else : Console.WriteLine("Remote machine is not reachable")
                End If
            Next
        Catch ex As Exception
            Console.Error.WriteLine(ex.Message.ToString())
        Finally
            Console.ReadLine()
        End Try
End Sub

End Module ​

7
  • Then please add the <!-- language: vb.net --> and a newline after that again before the code block? Why is this suddenly css code? Why did you change the "\\" to "\"? It's correct in Visual Basic, it was supposed to access an SMB share with \\someIP\path I guess. Commented Dec 5, 2015 at 20:24
  • @jdweng - I edited the code as per te above modifications, but i have a problem, whenever remote machine is not reachable it's not displaying "Remote machine is not reachable" instead it's showing An Exception occurred during a ping request and my program will stop. Commented Dec 5, 2015 at 20:30
  • @MaximilianGerhardt - it was modified by jdweng Commented Dec 5, 2015 at 20:35
  • Then make sure to also wrap the My.Computer.Network.Ping() call into a try-catch if it can throw exceptions. By the way, for the "executing things in parallel", you chould check out Parallel.ForEach() (msdn.microsoft.com/de-de/library/…) Commented Dec 5, 2015 at 20:35
  • @MaximilianGerhardt - I have edited with try-catch but am getting the same message:An Exception occurred during a ping request Commented Dec 5, 2015 at 20:42

1 Answer 1

0

I know you shouldn't just dump code in a stackoverflow question, but here's a reference solution:

Imports System.IO
Imports System.Threading
Imports System.Threading.Tasks

Module Module1

    Dim RemoteComputer As New List(Of String)
    Const FILENAME As String = "c:\temp\test.txt"

    Sub Main()
        Dim reader As New StreamReader(FILENAME)
        While reader.EndOfStream = False
            RemoteComputer.Add(reader.ReadLine)
        End While
        Parallel.ForEach(Of String)(RemoteComputer,
                                    Sub(ComputerName As String)
                                        Try
                                            Dim IPAddress As String = ComputerName.ToString.Substring(0, 13).ToString()
                                            Dim CName As String = ComputerName.ToString.Substring(14, 6).ToString()
                                            Dim ZipToCreate As String = "\\" & IPAddress & "\C$\temp\Test-" & CName & ".zip"
                                            If My.Computer.Network.Ping(Trim(IPAddress.ToString())) Then
                                                Using zip As ZipFile = New ZipFile
                                                    Dim filenames As String() = System.IO.Directory.GetFiles("\\" & IPAddress & "\C$\Sample\")
                                                    For Each file In filenames
                                                        zip.AddFile(file, "")
                                                    Next
                                                    zip.Save(ZipToCreate)
                                                    File.Copy("\\" & IPAddress & "\C$\temp\Test-" & CName & ".zip", "\\destSeverIP\C$\Temp\Test-" & CName & ".zip", True)
                                                    Console.WriteLine("File copied successfully.....")
                                                End Using
                                            Else
                                                Console.WriteLine("Remote machine is not reachable")
                                            End If
                                        Catch ex As Exception
                                            Console.Error.WriteLine(ex.Message.ToString())
                                        Finally
                                            Console.ReadLine()
                                        End Try
                                    End Sub)
    End Sub
End Module
Sign up to request clarification or add additional context in comments.

7 Comments

it works fine but there's a little bug here, when i run the program.. it executed all in parallel, however is it possible to make the program pick up 5 each at a time ??
You can control the level of parallelism with an override of the Parallels.ForEach method, one parameter is an ParallelOptions object which has a MaxDegreeOfParallelism property, which controls how many objects are at most processed at once. See msdn.microsoft.com/de-de/library/dd783747%28v=vs.110%29.aspx and msdn.microsoft.com/de-de/library/… for that. So all in all, you just need to change the starting line to Parallel.ForEach(Of String)(RemoteComputer, New ParallelOptions() With {.MaxDegreeOfParallelism = 5},
Is it possible to save the output of these parallel execution as log for tracking purpose?
Sure, just append every message or error into file, or a variable. You can define a static volatile string log_messages somewhere in the class and append into that string one at a time. Be careful with files however, you don't want to write to the same file at the exact same moment. Concurrency is not to be taken lightly.
Thanks so much for your effort in helping me out :0 really appreciated.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.