[fpc-devel] Running programs with output redirection
Ludo Brands
ludo.brands at free.fr
Sun Jul 17 08:52:37 CEST 2011
> There seems to exist an issue with (output) redirection, when
> I try to
> run an external program from code. What I want to achieve is this:
>
> In a Win32 console I can run
> >diff -r dir1 dir2 > diff.txt
>
> When I use e.g. Exec('diff', args), diff complains about an excess
> argument, it works when "> diff.txt" is removed. With other
> functions I
> had less luck (ExecuteProcess, CreateProcess...), no output at all.
>
> Now I assume that output redirection is a shell feature, not
> available
> in direct invocation of an external program. Right?
>
Redirection is indeed a shell function.
> 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.
Ludo
More information about the fpc-devel
mailing list