Jump to content
Tuts 4 You

Copying A Fille U/ Dynamic Array


0xFF

Recommended Posts

a very simple procedure (add to private declaration unless other units need access to it) that'll read and write 1 byte each time using "for" loop to the file size, just look and read comments.

This supports a progress bar for user interface ;]

procedure TMainForm.Copy_File(Source, Dest: PChar; ProgressBar: TProgressBar);
var
MyArray: Array Of Byte; //Dynamic Array Of Byte
hRead, hWrite: THandle; //Handles returned by CreateFile, usage for Reading file size and writing the new file lpNumberOfBytesWritten: Cardinal; //Store how many bytes have been written
lpNumberOfBytesReaded: Cardinal; //Store how many bytes have been readed nFileSize: Integer; //Stores the size of the file to be copied
i: Integer; //Temp variable for the use of "for" loop
begin
hRead := CreateFile(source, GENERIC_READ, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); //Open the file with GENERIC_READ access
nFileSize := GetFileSize( hRead, nil ); //Reads the file size
ProgressBar.Max := nFileSize; //We set the maximum position of the progressbar according to the file size (int)
SetLength( MyArray, nFileSize ); //Set the size of the dynamic array within file size (bytes)
hWrite := CreateFile(dest, GENERIC_WRITE, 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);for i := 0 to nFileSize-1 do
begin
Application.ProcessMessages;
ProgressBar.Position := i;
ReadFile( hRead, MyArray[i], sizeof(MyArray[i]), lpNumberOfBytesReaded, nil);
WriteFile( hWrite, MyArray[i], sizeof(MyArray[i]), lpNumberOfBytesWritten, nil);
end;//Cleanup routine...
Progressbar1.Position := 0;
//Close handles when done using them (so other programs can have access, unless you use SHARED_ACCESS)
CloseHandle( hRead );
CloseHandle( hWrite );
end;
Edited by Rot1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...