How to execute multiple cmd commands in VB.net

Asked

Viewed 6,793 times

2

I know that you can execute a cmd command through VB.Net, with Shell in VB.NET:

Shell("CMD.exe /c {comando cmd aki}")

However, when you have to use a command like cd to change the directory, it seems not possible with the above code. Explaining better, I want to run a batch file type (.bat), only that do not want to use an external file, I want to use only the application in VB.net for this. I will give an example in a pseudo-code:

Shell("CMD.EXE /c cd C:\LocalPath")
ContinuaShellAnterior("mkdir Locale\")
ContinuaShellAnterior("cd Locale\")

Do you understand? In this case, this code would:

  1. Access the directory C: Localpath
  2. Would create a directory within the C: Localpath directory, called Locale
  3. Would access the directory C: Localpath Locale\

I can understand what I want?

  • what’s wrong with putting the complete directory when using mkdir?

  • No, you just don’t have to, because I’ll be inside the directory where I want to create it anyway. :)

3 answers

3

You can use the method Sys() from the "Systemexpansion" class library, it offers additional commands for namespace System. See the example how to use it:

Sys("Help") ' Vai mostrar os comandos do console...

Or if you want, you can use this method and invoke it to place the command:

Public Sub Sys(ByVal CmdCommand As String)
    Dim generated As String = (IO.Path.GetTempPath & "\" & IO.Path.GetRandomFileName() & ".bat")
    IO.File.Create(generated).Close()
    IO.File.WriteAllText(generated, "@echo off" & vbNewLine & vbNewLine & CmdCommand)
    Shell(generated, AppWinStyle.Hide, True)
End Sub 
  • I really liked it! But the other one worked better :>)

  • Thank you haha :D

1


Just put a&' between the commands. In my case it would look like this:

Shell("CMD.EXE /c cd C:\LocalPath & mkdir Locale & cd Locale\")
  • 1

    This does not work in any case as it depends on the result of the previous command.

  • I asked in that case, quoted up there, so it does work! At least in this one I wanted, and that’s what matters!

  • 2

    It is good that you have solved in your case, but anyway it is important the warning, because another user may be looking for the solution to different commands, and have an unexpected result.

1

For this type of interaction you need a Process:

dim proc as ProcessStartInfo("cmd.exe")
dim pr as Process
pr=Process.Start(proc)
pr.StandardInput.WriteLine("comando1")
pr.StandardInput.WriteLine("comando2")

Follow a more complete excerpt from a reply from Sozão, see if it helps:

Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo

StartInfo.FileName = "cmd.exe"
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True

StartInfo.UseShellExecute = False

StartInfo.CreateNoWindow = False

myprocess.StartInfo = StartInfo
myprocess.Start()

Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput

SW.WriteLine("cd /D C:\batatinhas")
SW.WriteLine("comando")
SW.WriteLine("exit")

SW.Close()
SR.Close()

Do not forget that in these cases it is not to use /C in the cmd.exe

  • It didn’t work with me! That process is the component?

  • http://msdn.microsoft.com/pt-br/library/system.diagnostics.process(v=vs.110). aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.