0

I need to run a .bat file that I've previously generated with my program. I also need to run this batch file specifically on the system32/cmd.exe with the attributes "/k fdsinit". Any other ways of running this batch file get errors.

Could anyone help me? Thanks a lot. Best regards

First try:

        Process.Start("C:\Windows\System32\cmd.exe", "/k fdsinit"" ""cd " + Label6.Text + " ""mpiexec -n " + np.ToString() + " fds " + Label5.Text)

The commands contained in the string "cd ..." "mpiexec...." "fds...." are contained in the psi.Filename reference in the next tryout.

Second try:

        Dim psi As New System.Diagnostics.ProcessStartInfo()
        psi.Arguments = "C:\Windows\System32\cmd.exe /k fdsinit"
        psi.FileName = Label6.Text + "_runfds.bat"
        Dim prc As New System.Diagnostics.Process()
        prc.StartInfo = psi
        prc.Start()

EDIT

Following @Compo's comment, I've tried:

        Dim psi As New System.Diagnostics.ProcessStartInfo()
        psi.Arguments = "%ProgramFiles%\FireModels\FDS\bin\fdsinit.bat"
        psi.FileName = Label6.Text + "_runfds.bat"
        Dim prc As New System.Diagnostics.Process()
        prc.StartInfo = psi
        prc.Start()

Then I get once again the abovementioned algorhytm error (@Compo it's an hydra error, if you're familiar with FDS). I confirm you that I don't need the cd command, since the filename itself contains the full path of the batch file (Label6.text.....).

Thanks a lot to all will contribute!

  • Not sure exactly what you mean by _batch file specifically on the system32/cmd.exe_. Cmd is just a command line interface to execute other programs and seems nowhere in either sample are you running this batch file. While probably not "technically true", you run the batch file directly via Process.Start, it will execute it in the cmd because that is in effect the default program for bat files – Hursey Feb 21 '23 at 19:25
  • It seems to me as if your example is trying to run a sequence of commands within cmd.exe, not a batch file, as stipulated in your question. All batch files run by default in cmd.exe, so what is the problem with ```Process.Start("", "")```? – Compo Feb 21 '23 at 19:31
  • 1
    Your approach is incorrect. Just because those are the commands you may issue in a `cmd` window, doesn't mean that you're going to use all of them when using `Process`. Review the documentation for [System.Diagnostics.Process](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=net-7.0). The following may be helpful: https://stackoverflow.com/a/70927338/10024425, https://stackoverflow.com/a/75229470/10024425, https://stackoverflow.com/a/71344930/10024425, https://stackoverflow.com/a/70452809/10024425, and https://stackoverflow.com/a/75404929/10024425. – Tu deschizi eu inchid Feb 21 '23 at 19:32
  • The batch file contains the cd command (to reach the working directory) and the command to run the algorhytm. The manual way, that I want to automatize, to launch this algorhytm is through a shortcut of the cmd; when I reach the properties of this shortcut, under target I can see "C:\Windows\System32\cmd.exe /k fdsinit". Putting "/k fdsinit" as arguments of Process.Start, I receive an error from algorhytm. Also tried to put "fdsinit" into the batch file but when this command is processed, it seems that exits from the cmd and all the next commands are not executed. Hope to have been more clear – laflare99955 Feb 21 '23 at 23:02
  • 1
    It is not a batch file then! What you are trying to do is to run a sequence of commands in `cmd.exe` instead. Anyhow, you should not need the `CD` command at all. What you should be doing is defining that directory in VB.NET, e.g. ```psi.WorkingDirectory =```. When you run `cmd.exe` the first argument(s) should be `/D /K` or `/D /S /K`, then all of the cmd.exe commands/commandline you want to run should be enclosed within doublequotes e.g. `C:\Windows\System32\cmd.exe /D /K "Echo The current working directory is "%CD%”"`. _I deliberately doublequoted `%CD%` to show nested doublequotes_. – Compo Feb 21 '23 at 23:47
  • 1
    As a side note, because `fdsinit` is really `fdsinit.bat`, that command line should ideally read as, ```"%ProgramFiles%\FireModels\FDS\bin\fdsinit.bat"```, you should also, for robustness, instead of using `mpiexec -n`, use ```"%ProgramFiles%\FireModels\FDS\bin\mpiexec.exe" -n```. _(Please substitute your actual paths as needed.)_ – Compo Feb 22 '23 at 01:53
  • Another thing to watch out for since your arguments contains a path value. Wrap the text in double quotes to catch any spaces in the path. – Ryan Roos Feb 22 '23 at 22:21

0 Answers0