How to execute multiple cmd commands in VB.net

2

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

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

However, when you have to use command like cd, to change the directory, it does not seem possible with the above code. Explaining best, I want to execute a Batch file (.bat), only I do not want to use an external file, I want to use only the application in VB.NET for this. I'll give an example in a pseudo-code:

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

Understand? In case, this code would go:

  
  • Access the directory C: \ LocalPath
  •   
  • Would create a directory within the C: \ LocalPath directory, called Locale
  •   
  • Would access the directory C: \ LocalPath \ Locale \
  •   

    Did you understand what I want?

        
    asked by anonymous 12.01.2015 / 22:45

    3 answers

    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\")
    
        
    19.01.2015 / 01:56
    3

    You can use the Sys() method of the "SystemExpansion" class library, it offers additional commands for the System namespace. Here's 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 put 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 
    
        
    02.05.2015 / 06:54
    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")
    

    Here is a more complete excerpt from a SOHO response , see help:

    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 cmd.exe

        
    12.01.2015 / 23:08