Am I doing something silly here?
I've tried both the commandLine and parameter.Add methods to copy a file from
one location to another.
I can make it run using commandLine and robocopy command but dos commands do not seem
to run.
I've tried the copy command itself and also tried running cmd.exe (with its path) and adding the copy
command as a parameter with the proc.add() functions.
In all cases I get and E exception: 2 error.
In short - can I copy a file using the simple dos copy command with Tprocess somehow?
This code should produce my problem (ignore any obvious typos I had to type from another machine):
The lbFilesFound items (fpath variable) are a string line containing drive:path\filename.ext
deDestination.text is drive:path\
I have tried with and without trailing backslashes
procedure TFrmTest.SpeedButton1Click(Sender: TObject);
var
proc: Tprocess;
fpath: string;
i: integer;
action,param: string;
begin
TRY
if not (DirpathExists(deDestination.Text) ) then
begin
showmessage('Destination path must exist');
exit;
end;
if rbcopy.Checked then
action := 'COPY'
else
action := 'MOVE';
TRY
proc := TProcess.create(nil);
proc.CurrentDirectory := extractFileDir(fpath); // probably spurious
for i := 0 to lbFilesFound.Count -1 do
begin
fpath := trim(lbFilesFound.Items.Strings[i]) ;
if fpath = '' then continue;
param := action + ' ' + fpath + ' ' + deDestination.Text;
proc.CommandLine := param ; // This does not work.
{
// this works // proc.CommandLine:= 'c:\windows\system32\robocopy.exe ' + extractfileDir(fpath) + ' ' + deDestination.text + ' ' + extractFilename(fpath) + ' ' + action + ' /LOG+:c:\my.log /V /FP' ;
}
proc.Execute;
proc.WaitOnExit;
if proc.ExitStatus <> 1 then
begin
MessageDlg('Error',
'Error executing '+ action +#13
+'Error code: ' + IntToStr(proc.ExitStatus),
mtError,[mbCancel],0);
end;//exitStatus
end;//for i
FINALLY
proc.free;
END;
EXCEPT
on E: Exception do
begin
showmessage('Program E exception: ' + E.Message);
end;//on E
END;//except
end;//F