Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Delphi to execute a .bat file properly

I am running a .bat file from delphi(2010).

procedure TForm1.Button2Click(Sender: TObject);
var sCmd: String;
 Begin
sCmd := Pwidechar('b4a_c2dm.bat' +' ' +'send ' + Trim(Edit1.Text)+' '  + Trim(edit2.Text ));
ShellExecute(0, 'open', 'b4a_c2dm.bat', PChar(sCmd), nil, SW_SHOWMAXIMIZED);
   end;

This opens the cmd.exe and passes the correct string in the cmd.exe , BUT

Some how the line in the .bat file (java -cp b4a_c2dm.jar anywheresoftware.b4a.c2dm.C2DM %*) is showing up in the cmd.exe window and not letting the .bat file do its job.

Can someone help me with this.

like image 331
grant1842 Avatar asked May 05 '12 01:05

grant1842


People also ask

How do I run a batch file in Delphi?

In order to execute a batch file, the program to be called is 'cmd' and its parameter should be the name of the batch file. Try adding '/c' to the beginning of your parameter string, ie sCmd := Pwidechar('/c b4a_c2dm. bat'.... Also, if you are passing literals, you could do that in one string, '/c b4a_c2dm.

How do I run a bat file from anywhere?

bat from anywhere on the command line. Show activity on this post. Create a folder called Batches (lets say in your C drive). Append C:\Batches in your path environment variable and you then can run batch files in that directory from anywhere.


1 Answers

In order to execute a batch file, the program to be called is 'cmd' and its parameter should be the name of the batch file.

Regarding your program,

ShellExecute (application.handle, 'open', 'cmd', PChar(sCmd), nil, SW_MAXIMIZE)
like image 131
No'am Newman Avatar answered Oct 17 '22 19:10

No'am Newman