Board index » delphi » All I want to do is copy a file for goodnes sake!

All I want to do is copy a file for goodnes sake!

Have i got to use Windos API to copy a file? I canot find any Delphi file
managment functions to actually copy a file from a directory to another
directory.

there is an API call CopyAFile(dstination, source,  ,  );

but i do not know the unit to include.  It exists in the delphi help file
but there is nothing to say where it comes from.

Can anyone help?

Martin.

 

Re:All I want to do is copy a file for goodnes sake!


< i do not know the unit to include.>
It's not in WinProcs or WinTypes?

Brad
DBradDa...@aol.com

Re:All I want to do is copy a file for goodnes sake!


Gateway 2000 licenced user wrote:

Quote
> Have i got to use Windos API to copy a file? I canot find any Delphi
> file
> managment functions to actually copy a file from a directory to
> another
> directory.

> there is an API call CopyAFile(dstination, source,  ,  );

> but i do not know the unit to include.  It exists in the delphi help
> file
> but there is nothing to say where it comes from.

> Can anyone help?

> Martin.

 Ian Campbell pointed this out at a recent user group meeting.  I
haven't tried it, but he was quite sure it works.  Also, my syntax might
not be exact:

var
  t, s: TFileStream;
begin
  s := TFileStream.Create('SourceFile', fmOpenRead);
  t := TFileStream.Create('DestFile', fmOpenWrite or fmCreate);
  t.CopyFrom(s, s.size);
  s.free;
  t.free;
end;

Re:All I want to do is copy a file for goodnes sake!


Quote
DBradDavis wrote:

> < i do not know the unit to include.>
> It's not in WinProcs or WinTypes?

> Brad
> DBradDa...@aol.com

Welp, there is no method in the VCL to copy files. You can use this to
do it:

Procedure TfrmCPKEU.CopyFile( Const sourcefilename, targetfilename:
String );
var
        Stream : TMemoryStream;
begin
        try
        Stream := TMemoryStream.Create;
        Stream.LoadFromFile(sourcefilename);
        Stream.SaveToFile(targetfilename);
        Stream.Free;
        except
                on E : Exception do
                        begin
                        raise Exception.Create('Error copying file: ' +
E.Message);
                        end;
        end;
end;

--
+--------------------------------+
|        The Messiah             |
+--------------------------------+
|  Ou' sont les neiges d' antan  |
|         Villon                 |
+________________________________+
|    There is a man...           |
|   playing a violin...          |
|   and the strings...           |
| are the nerves in his own arm. |
| A twisted soul- the mortar...  |
|  despair- the bricks...        |
| to build a temple of sadness.  |
|  The Crow, J. O'Barr           |
+--------------------------------+

Re:All I want to do is copy a file for goodnes sake!


Quote
DBradDavis wrote:

> < i do not know the unit to include.>
> It's not in WinProcs or WinTypes?

Actually, if you include "Windows", then you can use the CopyFile or
MoveFile API commands...  For CopyFile, try this:

If Not(CopyFile(pChar(Source), pChar(Dest), False)) then
Begin
   ... handle error (retrieved through "GetLastError" command)
End;

Note:  That code would automatically overrite the destination file if it
existed.  If you didn't want to that to happen, then change that "False"
to a "True" and then make sure you look for the ERROR_FILE_EXISTS (I
think).

Hope that works out for ya...

Dave

Re:All I want to do is copy a file for goodnes sake!


Look on the superpage for a FileCopy component,
Why invent it yourself???
--
+++

Use this address to Mail, no Spam_Mail.
Sanne Hoekstra
Sant...@dds.dds.nl

David Culbertson <culb...@hlc.net> schreef in artikel
<33937E0B.5...@hlc.net>...

Quote
> DBradDavis wrote:

> > < i do not know the unit to include.>
> > It's not in WinProcs or WinTypes?

> Actually, if you include "Windows", then you can use the CopyFile or
> MoveFile API commands...  For CopyFile, try this:

> If Not(CopyFile(pChar(Source), pChar(Dest), False)) then
> Begin
>    ... handle error (retrieved through "GetLastError" command)
> End;

> Note:  That code would automatically overrite the destination file if it
> existed.  If you didn't want to that to happen, then change that "False"
> to a "True" and then make sure you look for the ERROR_FILE_EXISTS (I
> think).

> Hope that works out for ya...

> Dave

Re:All I want to do is copy a file for goodnes sake!


I smell VB Converts....   try using windows  

Use the winapi call         BOOL    FileCopy(LPCSTR lpexistingfilename,LPCSTR lpnewfilenae,BOOL bFailifexists)

or in Pascal
USES Windows;

       Function  FileCopy (lpexistingfilename , lpnewfilenae : PChar ;  bFailifexists : Boolean) : Boolean;

Send me a thank you.
Nix.

Quote
"Sanne Hoekstra" <NO_MORE_SPAM_Sant...@dds.dds.nl> wrote:
>Look on the superpage for a FileCopy component,
>Why invent it yourself???
>--
>+++

>Use this address to Mail, no Spam_Mail.
>Sanne Hoekstra
>Sant...@dds.dds.nl

>David Culbertson <culb...@hlc.net> schreef in artikel
><33937E0B.5...@hlc.net>...
>> DBradDavis wrote:

>> > < i do not know the unit to include.>
>> > It's not in WinProcs or WinTypes?

>> Actually, if you include "Windows", then you can use the CopyFile or
>> MoveFile API commands...  For CopyFile, try this:

>> If Not(CopyFile(pChar(Source), pChar(Dest), False)) then
>> Begin
>>    ... handle error (retrieved through "GetLastError" command)
>> End;

>> Note:  That code would automatically overrite the destination file if it
>> existed.  If you didn't want to that to happen, then change that "False"
>> to a "True" and then make sure you look for the ERROR_FILE_EXISTS (I
>> think).

>> Hope that works out for ya...

>> Dave

Re:All I want to do is copy a file for goodnes sake!


PS Err thats copyfile..

I smell VB Converts....   try using windows  

Use the winapi call         BOOL    FileCopy(LPCSTR lpexistingfilename,LPCSTR lpnewfilenae,BOOL bFailifexists)

or in Pascal
USES Windows;

       Function  FileCopy (lpexistingfilename , lpnewfilenae : PChar ;  bFailifexists : Boolean) : Boolean;

Send me a thank you.
Nix.

Quote
"Sanne Hoekstra" <NO_MORE_SPAM_Sant...@dds.dds.nl> wrote:
>Look on the superpage for a FileCopy component,
>Why invent it yourself???
>--
>+++

>Use this address to Mail, no Spam_Mail.
>Sanne Hoekstra
>Sant...@dds.dds.nl

>David Culbertson <culb...@hlc.net> schreef in artikel
><33937E0B.5...@hlc.net>...
>> DBradDavis wrote:

>> > < i do not know the unit to include.>
>> > It's not in WinProcs or WinTypes?

>> Actually, if you include "Windows", then you can use the CopyFile or
>> MoveFile API commands...  For CopyFile, try this:

>> If Not(CopyFile(pChar(Source), pChar(Dest), False)) then
>> Begin
>>    ... handle error (retrieved through "GetLastError" command)
>> End;

>> Note:  That code would automatically overrite the destination file if it
>> existed.  If you didn't want to that to happen, then change that "False"
>> to a "True" and then make sure you look for the ERROR_FILE_EXISTS (I
>> think).

>> Hope that works out for ya...

>> Dave

Other Threads