Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute command 2

Status
Not open for further replies.

kingduck

MIS
Oct 31, 2002
41
EG
Hi All;
How can i execute an external command from a consol application?

Thanks in advance
 
See System.Diagnostics.Process as this is the standard way of executing external applications.


Bob Boffin
 
Thanks Bob that realy helped. But, how do i issue internal DOS commands to the command prompt from within VB.NET. Like,

Varify on

Thanks
 
Use the Shell function to call the command processor:
Code:
Shell("cmd.exe /c xcopy C:\fred\*.* D:\charlie\")

You can put any DOS command after the /c and it will be executed. If any of the parameters to a command require quotes around them you'll have to double them up for it to work.


Bob Boffin
 
I use the following:

Code:
        Dim SetQueueProcess As Process = New Process
        Dim s As String
        SetQueueProcess.StartInfo.FileName = "your command.exe"
        SetQueueProcess.StartInfo.UseShellExecute = False
        SetQueueProcess.StartInfo.CreateNoWindow = True
        SetQueueProcess.StartInfo.RedirectStandardInput = True
        SetQueueProcess.StartInfo.RedirectStandardOutput = True
        SetQueueProcess.StartInfo.Arguments = "your command arguements"
        SetQueueProcess.Start()
        Dim sInput As StreamWriter = SetQueueProcess.StandardInput
        sInput.AutoFlush = True
        Dim sOutput As StreamReader = SetQueueProcess.StandardOutput
        s = sOutput.ReadToEnd()
        sInput.Close()
        sOutput.Close()
        SetQueueProcess.Close()
        If s <> "" Then
            MsgBox(s)
        End If

"s" will contain all the output from the command.
 
Thanks Bob and kevin for your reply
My command is;
"Rar.exe"

with the arguments
" a -rr2048 -t -m5 -isnd -r -t " & FileName & "D:\My Documents\"

where :
FileName = "i:\Back" & trim(str(Date.Today.DayOfWeek)) & ".rar"

I think that the long file name "My Documents" makes an error with rar it returns.

"No files found"

I used "mydocu~1" it worked fine

"My Documents" needs to be double quotes
How do i double quotes them?

Thanks
 
To put double quotes inside a string you need to repeat the " twice so your example would become:

Code:
" a -rr2048 -t -m5 -isnd -r -t " & FileName & """D:\My Documents\"""

This will produce the string:
Code:
 a -rr2048 -t -m5 -isnd -r -t i:\Back4.rar "D:\My Documents\"
Make sure that you have a space after the .rar (or before the "D:\My ...)



Bob Boffin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top