We help IT Professionals succeed at work.

using shell command in vb.net

peterdevadoss
on
5,630 Views
Last Modified: 2007-12-19
i am using a shell command in vb.net as follows:

        Try
            Shell("xcopy u:\*.* \\peter\pos /y")
        Catch ex As Exception
            MsgBox("Error copyin..")
        End Try

if error occured while copying (such as unable to access the folder), the error is not being catch.

how can i know that it is successful or not?
Comment
Watch Question

CERTIFIED EXPERT

Commented:
try:

Public Function startDoc(ByVal filepath As String, Optional ByVal args As String = "") As Boolean
        Try
            Process.Start(filepath, args)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

messagebox.show(startDoc( "xcopy", "u:\*.* \\peter\pos /y"))

hope this helps
Hi peterdevadoss,
According to documentation:

"The return value of the Shell function depends on whether the program named in Pathname is still executing when Shell returns. If you set Wait to True and the program finishes before the timeout expires, Shell returns zero. If the timeout expires, or if you omit Wait or set it to False, Shell returns the process ID of the program. The process ID is a unique number that identifies the running program. "

so i'll guess that you should wait until the program finish. I also doubt that you could get an exception from an xcopy command. I think you have to capture the shell return value and act accordly.

Cheers!
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
If this an example of what you are trying to do?  Otherwise, why use the Shell command to do something that you can do with System.IO stuff?

Author

Commented:
my requirement is as follows:
  1.   search all the computers in the network
  2.   search for a specified folder in each computer
  3.   if a specified folder found then copy files from server to that folder

I am doing this to update my software to all users in the network.

I am able to find each computers and copy the files successfully when i have access to that folder.  But I could not able to catch the error if it failed to copy the files.

following is the complete souce code:
------------------------------------------------------------------------------
Imports System.IO
Imports System.Net

Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim computerName As String

#Region " Windows Form Designer generated code "
   .
   .
#End Region

    Private Function IPtoHost(ByVal IPstr As String) As String
        Try
            Dim dns As System.Net.Dns
            Dim Host As New System.Net.IPHostEntry()
            Host = dns.GetHostByAddress(IPstr)
            computerName = Host.HostName
            'displays computer name
            RichTextBox1.Text = RichTextBox1.Text + Host.HostName
            'copy files if a computer found with this ip
            copyFiles()
        Catch err As Exception
            'exit function if no computers found with this ip
            Exit Function
        End Try
    End Function


    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)     Handles btnUpdate.Click
        Dim i As Integer
        'loop thru 11 to 14.. for testing only
        For i = 11 To 14
            IPtoHost("192.168.0." + Trim(Str(i)))
        Next
    End Sub

    Private Sub copyFiles()
        Try
            Shell("xcopy u:\*.* \\" & computerName & "\pos /y")
            'return successful msg to log
            RichTextBox1.Text = RichTextBox1.Text + "....... Successful."
        Catch ex As Exception
            'display error message if error occured while copying
            'it happened if, either the file being copied is in use or nor no access to that folder
            RichTextBox1.Text = RichTextBox1.Text + "....... Failed."
        End Try
        'add carriage return to log
        RichTextBox1.Text = RichTextBox1.Text + Chr(13)

    End Sub

End Class
------------------------------------------------------------------------------------------------------

Commented:
Why don't you simply perform a fso.filexists on the destination file after the copy? :-)

Barkis
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
ryancys's code returns only 'true' in any case

TheLearnedOne's code waits at console without endlessly
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
My bad.

Change this line:

processXCopy.StartInfo.Arguments = arguments

to read:

processXCopy.StartInfo.Arguments = "/c " & arguments

Author

Commented:
Thanks it  is working.  Is is possible to minisise the shell window while process.. .your answer will be appreciated.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.