[fpc-devel] Running programs with output redirection

Ludo Brands ludo.brands at free.fr
Sun Jul 17 09:14:20 CEST 2011


> > Does there exist a simple way to run "diff", and to redirect 
> > its output 
> > into an file? A batch file? Platform independence would be 
> > nice, of course.
> > 
> 
> Start the shell instead of diff:
> 
>   exec('cmd','/c diff -r dir1 dir2 > diff.txt');
> 
> The /c tells the shell to execute the command and exit immediately. 
> 

The above solution will show briefly the cmd window, the time of exexcuting
the diff. If you want to get rid of these popups you can use the following
alternative:

  ShellExecute(0, nil, 'cmd.exe', '/c diff -r dir1 dir2 > diff.txt', nil,
0);

You need to add shellapi to the uses clause. Note that ShellExecute returns
immediately and the new shell runs in the background, even when the program
stops. The new parent of the shell process is Explorer. Exec returns when
the shell ends.

This is all windows though.

Ludo




More information about the fpc-devel mailing list