-2

When I try to execute the cmd, the code inside the cmd is not getting executed. I expect that when I execute the cmd it should run the code inside that and does the operation. When I do by double-clicking the cmd file whole processes finish nicely. but through c#, the code inside the cmd file doesn't execute.

static void Main(string[] args)
{
    var proc = new Process();
    proc.StartInfo.FileName = @"D:\Dump\createAndDump.cmd";
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.Start();
    string outPut = proc.StandardOutput.ReadToEnd();
           
    proc.WaitForExit();
    var exitCode = proc.ExitCode;
    proc.Close();
}

the cmd has following code to execute: this connects to the tool and flashing of device is done. I am doing the code as stated above but it is not able to execute the lines inside the cmd file.

@echo connect> flash.mtb


@for %%i in (*.s11) do (

@echo open_file "%%~fi">> flash.mtb
@echo select_all_sections>> flash.mtb
@echo add_selected_sections>>flash.mtb
)

@echo program>> flash.mtb
@echo disconnect>> flash.mtb
@echo exit>> flash.mtb

for %%j in (flash.mtb) do "C:\Program Files (x86)\xyz\Memtool 4.7\IMTtool.exe" %%~dpj%%~nxj
Compo
  • 36,585
  • 5
  • 27
  • 39
AutoEng
  • 45
  • 11
  • What exactly is happening and how does that differ from what you expected? What information did you get while debugging your code? – devsmn Jun 26 '20 at 12:26
  • 1
    You should write your specific problem in the title. Pretend your talking with a busy colleague and you should summarize your problem in one sentence, then write the title. –  Jun 26 '20 at 12:28
  • When I try to execute the cmd, the code inside the cmd is not getting executed. I expect that when I execute the cmd it should run the code inside that and does the operation. When I do by double-clicking the cmd file whole processes finish nicely. but through c#, the code inside the cmd file doesn't execute. – AutoEng Jun 26 '20 at 12:30
  • Add your comment to body of your question and change the title to a specific question please –  Jun 26 '20 at 12:37
  • Possible duplicate: https://stackoverflow.com/q/5519328/107625 – Uwe Keim Jun 26 '20 at 12:44
  • 1
    It seems as if either your `C#` code should define the `WorkingDirectory`, or alternatively, your `.cmd` code should begin by defining it, (`%~dp0`). – Compo Jun 26 '20 at 12:48

1 Answers1

1

Obs.: The string in the path/code \xyz\, can be edited to \RealNameFolder\. Would it be necessary?

Considering:

  1. Yours *.s11 files are in the Dump folder:
 D:
  |
  \---Dump
          0001.s11
          0005.s11
          0010.s11
          0015.s11
  1. The folder for creating the flash.mtb file is \Dump
 D:
  |
  \---Dump
          0001.s11
          0005.s11
          0010.s11
          0015.s11
          flash.mtb
  1. The createAndDump.cmd file is one level before the \Dump folder:
 D:
  |
  |   createAndDump.cmd 
  |
  \---Dump
  1. The C# executable and createAndDump.cmd are in the same folder:
 D:
  |   createAndDump.cmd 
  |   RunBat.exe
  |
  \---Dump
  1. Use code suggested below to get the work directory where the executable is running, and use this to try call the bat:
string pathdir = Directory.GetCurrentDirectory();
             var process = new Process();
             var startinfo = new ProcessStartInfo(Path.Combine(pathdir, "createAndDump.cmd"));
  1. Can you please try to use the suggested code, to also signal you (producing a output), where it is easier to check what you have wrong, whether in your , or in the execution of your ?
process.OutputDataReceived += (sender, argsx) => Console.WriteLine(argsx.Data);

  • code:
 using System;
 using System.IO;
 using System.Diagnostics;
 
 namespace ConsoleApplication
 {
     class Program
     {
         static void Main(string[] args)
         {
 
             string pathdir = Directory.GetCurrentDirectory();
             var process = new Process();
             var startinfo = new ProcessStartInfo(Path.Combine(pathdir, "createAndDump.cmd"));
             startinfo.RedirectStandardOutput = true;
             startinfo.UseShellExecute = false;

             process.StartInfo = startinfo;
             process.OutputDataReceived += (sender, argsx) => Console.WriteLine(argsx.Data);
             process.Start();
             process.BeginOutputReadLine();
             process.WaitForExit();
         }
     }
 }

code:

You can try using cd /d "%~dp0", also use default Program files (ProgramFiles, ProgramFiles(x86), %ProgramW6432%) folders, to try find and get the full path to your \Memtool.exe file:

@echo off && setlocal EnableDelayedExpansion

cd /d "%~dp0" && pushd .\Dump && >"%~dp0Dump\flash.mtb" (
     echo\connect && for %%i in (*.s11)do echo\open_file "%%~fi" && echo\select_all_sections && echo\add_selected_sections
     echo\program && echo\disconnect && echo\exit ) || echo. Yes, something is very wrong here... && endlocal && goto :EOF
     
popd && (for %%i in (ProgramFiles,ProgramFiles(x86^),ProgramW6432)do if /i defined %%~i 2>nul (for /f tokens^=^* %%M in ('
     %__APPDIR__%where.exe /r "!%%~i!\xyz\Memtool 4.7" "IMTtool.exe"')do echo/%%M|%__APPDIR__%findstr.exe /ei xe>nul && (
     pushd "!%%~i!\xyz\Memtool 4.7" && "!%%~i!\xyz\Memtool 4.7\%%~nxM" "%~dp0Dump\flash.mtb" && endlocal && goto :EOF )))
Io-oI
  • 2,514
  • 3
  • 22
  • 29
  • @AutoEng Is the bat called in the same folder where you have the `*.s11` files? – Io-oI Jun 30 '20 at 10:50
  • This is also what I found as an issue. As my project is huge and carry the legacy, the compiler was searching for s11 file on one specific folder while doing debug, I thought it to be in debug folder inside the bin of solution(default), but code for finding the file was done by some developer long back and was hardcoded(don' know why :P). This was only the issue and both the shell scripts yours and mine worked after placing the files in folder. Thanks. :) Nice to see you got the question and also answered to the point else I am only getting -1 to my question and no answers. lol :D – AutoEng Jun 30 '20 at 13:03
  • @AutoEng For help proposal, 1st point of understanding or problem, here are people who would easily help you, but some difficulty in understanding the problem resulted in downs votes – Io-oI Jun 30 '20 at 13:11