Solved

Copy file

Posted on 2004-03-30
16
8,383 Views
Last Modified: 2010-02-21
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!
0
Comment
Question by:kmorris1186
  • 5
  • 4
  • 3
  • +2
16 Comments
 
LVL 4

Accepted Solution

by:
k4hvd77 earned 75 total points
ID: 10712065
hi,
use this to copy a file: copyfile(PChar('SourcePathFileNameHere'),PChar('TargetPath[FileName]Here'),True)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10712104
found this in my paq

uses shellapi;

procedure TForm1.Button1Click(Sender: TObject);
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.fAnyOperationsAborted := False; //obsolete
  opInfo.lpszProgressTitle := 'Moving . . .';  //foo simpleprogressdisplay
  opInfo.Wnd := self.handle;  //the parent of the progressdisplay
  SHFileOperation(opInfo);  //do it
end;

meikl ;-)


0
 
LVL 4

Expert Comment

by:k4hvd77
ID: 10712115
meikl,
I thought he need to copy singel files "I have looked around and cant find any way to copy single files" !
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10712165
hmm, yes, you're correct, k4
0
 
LVL 7

Author Comment

by:kmorris1186
ID: 10712167
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(strDest),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.)
0
 
LVL 7

Author Comment

by:kmorris1186
ID: 10712181
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?
0
 
LVL 4

Expert Comment

by:k4hvd77
ID: 10712241
no, you don't need anythig more in Uses Section.
0
 
LVL 4

Expert Comment

by:k4hvd77
ID: 10712255
or something like that:

procedure Copyfile (strSource : String; strDest : String; Overwrite : Boolean );
begin
   if Overwrite then
      begin
         copyfile(PChar(strSource),PChar(strDest),True);
      end
      else
      begin
         copyfile(PChar(strSource),PChar(strDest),False);  
      end;
end;

   
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Author Comment

by:kmorris1186
ID: 10712303
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.)
0
 
LVL 4

Expert Comment

by:k4hvd77
ID: 10712334
try it later and accept my answer if it works ;)
0
 
LVL 9

Assisted Solution

by:ginsonic
ginsonic earned 50 total points
ID: 10713658
CopyFileWithProgressBar(Path+'test.txt','a:\test.txt' );

////////////
private
   procedure CopyFileWithProgressBar( Source, Destination : string );

/////////

procedure TForm1.CopyFileWithProgressBar( Source, Destination : string);
var
  FromF, ToF : file of byte;
  Buffer : array[ 0..4096 ] of char;
  NumRead : integer;
  FileLength : longint;
begin
  AssignFile( FromF, Source );
  Reset( FromF );
  AssignFile( ToF, Destination);
  Rewrite( ToF );
  FileLength := FileSize( FromF );


  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;
    CloseFile( FromF );
    CloseFile( ToF );

  end;   // delete if don't wish to show the progress
end;
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 10713668
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.
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 10713710
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.
 
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10713732
well, my version :-))

var
  source, target : TFileStream;
begin
  source := TfileStream.Create('SourcePathFileNameHere',fmOpenRead);
  target := TfileStream.Create('TargetPathFileNameHere',fmCreate);
  target.Copyfrom(source,0);
  target.free;
  source.free;
end;

just from head

meikl ;-)
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10715304
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
0
 
LVL 7

Author Comment

by:kmorris1186
ID: 10746261
k4hvd77 - Thanks! that will do what i need.

ginsonic - Thanks for the extra info! i should be able to use it.



0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JSON on ISAPI REST Server using Delphi 2 229
PDF library for Delphi 2 104
Convert Jpg /PNG To GIF 5 118
tvirtualstringtree freeze when load too manny images 10 52
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now