kmorris1186
asked on
Copy file
Hi all,
I am new to Delphi but am migrating from VB. It is a big change. I have a program that pulls Zip files from a CD (it gets this list from a Access 97 Database) and puts then in a directory. I have looked around and cant find any way to copy single files. I found many resources on copying directorys, but not single files. I found somthing on the "SHFileOpStruct", but being new to Delphi i have no idea what it is. Guessing it is somthing similar to the FileSystemObject.
Please give me some examples with as many comments as you can fit in there. It will help me understand it. Goes from reading Visual basic to Delphi is rough (for me anyway).
Thanks!
I am new to Delphi but am migrating from VB. It is a big change. I have a program that pulls Zip files from a CD (it gets this list from a Access 97 Database) and puts then in a directory. I have looked around and cant find any way to copy single files. I found many resources on copying directorys, but not single files. I found somthing on the "SHFileOpStruct", but being new to Delphi i have no idea what it is. Guessing it is somthing similar to the FileSystemObject.
Please give me some examples with as many comments as you can fit in there. It will help me understand it. Goes from reading Visual basic to Delphi is rough (for me anyway).
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
meikl,
I thought he need to copy singel files "I have looked around and cant find any way to copy single files" !
I thought he need to copy singel files "I have looked around and cant find any way to copy single files" !
hmm, yes, you're correct, k4
ASKER
k4hvd77 - so if i wanted to copy a file... i could do this :
procedure TForm1.Button1Click(Sender : TObject);
var
strSource : String;
strDest : String;
begin
strSource := edit1.text // user entered 'c:\testfile.txt'
strDest := edit2.text // user entered 'd:\testdir\'
copyfile(PChar(strSource), PChar(strD est),True)
end;
That should copy c:\testfile.txt to d:\testdir\testfile.txt correct? (i am not at a box with Delphi right now, sorry.)
procedure TForm1.Button1Click(Sender
var
strSource : String;
strDest : String;
begin
strSource := edit1.text // user entered 'c:\testfile.txt'
strDest := edit2.text // user entered 'd:\testdir\'
copyfile(PChar(strSource),
end;
That should copy c:\testfile.txt to d:\testdir\testfile.txt correct? (i am not at a box with Delphi right now, sorry.)
ASKER
and one more thing. Am i going to have to add any thing in the USES section? or is this a pocedure that is built in to delphi?
no, you don't need anythig more in Uses Section.
or something like that:
procedure Copyfile (strSource : String; strDest : String; Overwrite : Boolean );
begin
if Overwrite then
begin
copyfile(PChar(strSource), PChar(strD est),True) ;
end
else
begin
copyfile(PChar(strSource), PChar(strD est),False );
end;
end;
procedure Copyfile (strSource : String; strDest : String; Overwrite : Boolean );
begin
if Overwrite then
begin
copyfile(PChar(strSource),
end
else
begin
copyfile(PChar(strSource),
end;
end;
ASKER
ok.. since i am not at a box with delphi, Will this work with a UNC path?
(either way, you get the points tomorrow when i get to work to test this out.)
(either way, you get the points tomorrow when i get to work to test this out.)
try it later and accept my answer if it works ;)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
My code copy a single file from source to destination. In same time let you to know the copy stage ( progress ).
If don't wish to know it I mark what to delete as just to copy.
If don't wish to know it I mark what to delete as just to copy.
Me again. :P Look better if use :
8<----
with ProgressBar do begin // delete if don't wish to show the progress
Min := 0; // delete if don't wish to show the progress
Max := FileLength; // delete if don't wish to show the progress
while FileLength > 0 do begin
BlockRead( FromF, Buffer[ 0 ], SizeOf( Buffer ), NumRead );
FileLength := FileLength - NumRead;
BlockWrite( ToF, Buffer[ 0 ], NumRead );
Position := Position + NumRead;
end;
end; // delete if don't wish to show the progress
CloseFile( FromF );
CloseFile( ToF );
--------->8
The code work perfect in one of my projects. In same time, I use a procedure to verify if I make a right copy. If interested you ...ask.
8<----
with ProgressBar do begin // delete if don't wish to show the progress
Min := 0; // delete if don't wish to show the progress
Max := FileLength; // delete if don't wish to show the progress
while FileLength > 0 do begin
BlockRead( FromF, Buffer[ 0 ], SizeOf( Buffer ), NumRead );
FileLength := FileLength - NumRead;
BlockWrite( ToF, Buffer[ 0 ], NumRead );
Position := Position + NumRead;
end;
end; // delete if don't wish to show the progress
CloseFile( FromF );
CloseFile( ToF );
--------->8
The code work perfect in one of my projects. In same time, I use a procedure to verify if I make a right copy. If interested you ...ask.
well, my version :-))
var
source, target : TFileStream;
begin
source := TfileStream.Create('Source PathFileNa meHere',fm OpenRead);
target := TfileStream.Create('Target PathFileNa meHere',fm Create);
target.Copyfrom(source,0);
target.free;
source.free;
end;
just from head
meikl ;-)
var
source, target : TFileStream;
begin
source := TfileStream.Create('Source
target := TfileStream.Create('Target
target.Copyfrom(source,0);
target.free;
source.free;
end;
just from head
meikl ;-)
Hi everybody, my version works under both Linux and MS Windows:
Delphi syntax: function CopyFileTo(const Source, Destination: string): Boolean;
Example:
uses
.... IdGlobal;
if CopyFileTo(SourceFileName, TargetFileName) then
begin
// The copy is successful
end;
emil
Delphi syntax: function CopyFileTo(const Source, Destination: string): Boolean;
Example:
uses
.... IdGlobal;
if CopyFileTo(SourceFileName,
begin
// The copy is successful
end;
emil
ASKER
k4hvd77 - Thanks! that will do what i need.
ginsonic - Thanks for the extra info! i should be able to use it.
ginsonic - Thanks for the extra info! i should be able to use it.
uses shellapi;
procedure TForm1.Button1Click(Sender
var I : Integer;
opInfo : _SHFILEOPSTRUCT;
begin
opInfo.pFrom := 'd:\TmpFolder'; //SourceFolder
opInfo.pTo := 'u:\'; //DestinationFolder
//opInfo.wFunc := FO_MOVE; //Move it
//opInfo.wFunc := FO_COPY; //Copy it
opInfo.fFlags := FOF_NOCONFIRMATION or //no confirmation
FOF_NOCONFIRMMKDIR or
//no confirmation FOF_RENAMEONCOLLISION or //rename if dest exist
FOF_SIMPLEPROGRESS; //show a progress
opInfo.hNameMappings := nil; // no mappings
opInfo.fAnyOperationsAbort
opInfo.lpszProgressTitle := 'Moving . . .'; //foo simpleprogressdisplay
opInfo.Wnd := self.handle; //the parent of the progressdisplay
SHFileOperation(opInfo); //do it
end;
meikl ;-)