Ads 468x60px

Smaller time frame always follow the bigger time frame. It's better wait be patience to enter in position than risk with BIG SL. Having strong trading discipline and taking losses when necessary is a sign of serious trading approach

Friday, April 27, 2012

Run command line from delphi

how can i run this command from my delphi
C:\myapppath\appfolder>appname.exe /stext save.txt
i tried like this
ShellExecute(0, nil, 'cmd.exe', 'cd C:\myapppath\appfolder', nil, SW_Hide);
ShellExecute(0, nil, 'cmd.exe', 'appname.exe /stext save.txt', nil, SW_Hide);
but not working
any help please
thanks in advance

1 Answer

up vote 3 down vote accepted
To run a CMD command, you need to use the /C flag of cmd.exe:
ShellExecute(0, nil, 'cmd.exe', '/C cd C:\myapppath\appfolder', nil, SW_HIDE);
ShellExecute(0, nil, 'cmd.exe', '/C appname.exe /stext save.txt', nil, SW_HIDE);
However, this will create two different sessions, so it will not work. But you can use ShellExecute to run appname.exe directly, like so:
ShellExecute(0, nil, 'appname.exe',  '/stext save.txt', nil, SW_HIDE);
But you need to specify the filenames properly.
I would do
  path: string;
  path := ExtractFilePath(Application.ExeName);
  ShellExecute(0, nil, PChar(Application.ExeName), PChar('/stext "' + path + 'save.txt"'), nil, SW_HIDE);
in case appname.exe is the current application. Otherwise, replace Application.ExeName with the full path of appname.exe.