Jump to content
Delphied

ShellExecute Command Line Program Save Output to Log File

Recommended Posts

I am trying to execute a command line program located in a folder that the user sets and it doesn't appear to work.  The Windows command is as follows:

 

C:\Users\<username>\Desktop\Winlibimobile\idevice_id.exe > C:\Users\<username>\Desktop\Logs\idevice_id.log

 

When I enter this in the command line I get the output in the idevice log file in the location specified.  But when I user shellexecute it does not work.

 

procedure TfrmMdp.btnExtractClick(Sender: TObject);
var
  DeviceIdCmd : string;
begin
  DeviceIdCmd := ExePath + '\idevice_id.exe > ' + LogPath + '\idevice_id.log';
  ShowMessage(DeviceIdCmd);
  //ShellExecute(0, nil, 'cmd.exe', PChar(DeviceIdCmd), nil, SW_HIDE); <---this also did not work
  ShellExecute(Handle ,'open', PChar(DeviceIdCmd), '', nil, SW_SHOWNORMAL);
end;

 

Is there something I'm missing?  Thanks in advance!

Share this post


Link to post

I found a crude work around.  I wrote a batch script with the needed command and used DosCommand to execute it and it worked!  Thinking I could create the batch script in code, so user selection for directories are correct and execute that then delete the batch script so it doesn't interfere with the next run.

Share this post


Link to post

 

Command line redirection is performed by the command line interpreter

 

Try adding the /C parameter when invoking cmd.exe directly:

DeviceIdCmd := '/C ' + AnsiQuotedStr(ExePath + '\idevice_id.exe', '"') + ' > ' + AnsiQuotedStr(LogPath + '\idevice_id.log', '"');
ShellExecute(0, nil, 'cmd.exe', PChar(DeviceIdCmd), nil, SW_HIDE);

Alternatively, use CreateProcess() to execute idevice_id.exe directly and capture it's output to write to the log file yourself:

 

Creating a Child Process with Redirected Input and Output

Edited by Remy Lebeau

Share this post


Link to post

As a broad rule, you should never call ShellExecute. It's long since been replaced by ShellExecuteEx. The only reason you need to know why, is that ShellExecute doesn't report errors correctly. ShellExecuteEx does.

 

And in this case, as in so many cases, as Remy already pointed out, CreateProcess is correct. Whenever you are creating a new process, you use CreateProcess and not ShellExecuteEx. The latter is for executing shell verbs.

Share this post


Link to post

Excellent,  thanks all I will give it a shot and see how it goes!  Really appreciate the help!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×