Recent

Author Topic: CopyFile does not copy the file  (Read 4654 times)

Birger52

  • Sr. Member
  • ****
  • Posts: 309
CopyFile does not copy the file
« on: February 13, 2020, 12:01:32 pm »
I have this:
Code: Pascal  [Select][+][-]
  1. procedure TMyStripForm.BackupFile(aFile:string); // Laver backup af databasen
  2. var
  3.   idx : integer;
  4.   oName, bName : string;
  5. begin
  6.   for idx := 2 downto 0 do begin
  7.     oName := aFile;
  8.     if idx > 0 then oName += '.backup_'+IntToStr(idx);
  9.     if FileExists(oName) then begin
  10.       bName := aFile+'.backup_'+IntToStr(idx+1);
  11.       CopyFile(oName, bName, [cffPreserveTime, cffOverwriteFile]);
  12.     end;
  13.   end;
  14. end;

And it has been functioning.
So - needed to use he backup.
Deleted original and renamed .backup_1 to original.
Kind of the whole idea.
and it also functioned.
With the exception, that the original is now no longer copied to .backup_1

from
https://wiki.freepascal.org/CopyFile
Code: Pascal  [Select][+][-]
  1. type
  2.  TCopyFileFlag = (
  3.    cffOverwriteFile,
  4.    cffCreateDestDirectory,
  5.    cffPreserveTime
  6.    );
  7.  TCopyFileFlags = set of TCopyFileFlag;
  8.  
  9. function CopyFile(const SrcFilename, DestFilename: string): boolean;
  10. function CopyFile(const SrcFilename, DestFilename: string; PreserveTime: boolean): boolean;
  11. function CopyFile(const SrcFilename, DestFilename: string; Flags: TCopyFileFlags=[cffOverwriteFile]): boolean;

Anybody has a workaround - or an explanation for that matter?

Lazarus 2.0.8 FPC 3.0.4
Win7 64bit
Playing and learning - strictly for my own pleasure.

Birger52

  • Sr. Member
  • ****
  • Posts: 309
Re: CopyFile does not copy the file
« Reply #1 on: February 13, 2020, 12:06:52 pm »
Tried altering the code a bit..
Code: Pascal  [Select][+][-]
  1.       if not CopyFile(oName, bName, [cffPreserveTime, cffOverwriteFile]) then
  2.         Msg.DispMsg('Error backing up database'+sLineBreak+oName, mtErr);
  3.  

Which reveals, that CopyFile does not only not copy the file - it also returns true, as if the file was actually copied...
Lazarus 2.0.8 FPC 3.0.4
Win7 64bit
Playing and learning - strictly for my own pleasure.

shadow

  • New member
  • *
  • Posts: 9
Re: CopyFile does not copy the file
« Reply #2 on: February 13, 2020, 01:18:42 pm »
Do you have the fully qualified name given?
Example: "C:\Temp\myfile.txt" -> "C:\Temp\myfile.txt.backup"

Otherwise the file will be copied to the current working direktory.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: CopyFile does not copy the file
« Reply #3 on: February 13, 2020, 03:09:10 pm »
For debugging purposes, try with this to see what it's doing:

Code: Pascal  [Select][+][-]
  1.           if CopyFile(oName, bName, [cffPreserveTime, cffOverwriteFile]) then
  2.             ShowMessageFmt('Backed %s to %s',[oName, bName])
  3.           else
  4.             Msg.DispMsg('Error backing up database'+sLineBreak+oName, mtErr);
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 11915
Re: CopyFile does not copy the file
« Reply #4 on: February 13, 2020, 03:30:11 pm »
Are you sure that the file is not copied? Windows sometimes has the nasty habit to not update the Explorer when files are copied/deleted from outside the Explorer. In the Explorer, press F5 after your code has been executed.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: CopyFile does not copy the file
« Reply #5 on: February 13, 2020, 03:57:56 pm »
One hint off topic:

Don't use the Windows Explorer at all - it has a lot of errors and missing features.

I use the Total Commander - not allowed to call it Windows Commander anymore (M$ §§).

It's a swiss knife made in switzeland.

https://www.ghisler.com/deutsch.htm


Winni

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: CopyFile does not copy the file
« Reply #6 on: February 14, 2020, 11:17:06 am »
One hint off topic:

Don't use the Windows Explorer at all - it has a lot of errors and missing features.
Winni

Off topic²
Don't use Windows at all.....
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: CopyFile does not copy the file
« Reply #7 on: February 14, 2020, 12:00:58 pm »
One hint off topic:

Don't use the Windows Explorer at all - it has a lot of errors and missing features.
Winni

Off topic²
Don't use Windows at all.....

It's like the black death in 1500.
And only a few use the remedy     ......

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: CopyFile does not copy the file
« Reply #8 on: February 14, 2020, 12:16:50 pm »
One hint off topic:

Don't use the Windows Explorer at all - it has a lot of errors and missing features.
Winni

Off topic²
Don't use Windows at all.....

It's like the black death in 1500.
And only a few use the remedy     ......
Yeah, on Wednesday i set up a computer with Manjaro Architect with Cinnamon DE from scratch within 30 Minutes, and the same day a friend asked me to setup his new Laptop with Windows 10......
I think the Laptop was done Thursday morning......
"... Hi i'm Cortana, i'm going to help you with the setup.." .."?!?!? SHUT BLOODY HELL UP!"..."Do you want to send Data to Microsoft"..."SHUT UP!! NO TO ALL OPTIONS!!"...."Are you sure?"
At that moment i was looking to the window, and wondering if i could score a 3-pointer from 20 feet away.......
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: CopyFile does not copy the file
« Reply #9 on: February 17, 2020, 09:18:34 am »
Thanks for tip on Total Commander. A lot better than Exployer.

One hint off topic:

Don't use the Windows Explorer at all - it has a lot of errors and missing features.

I use the Total Commander - not allowed to call it Windows Commander anymore (M$ §§).

It's a swiss knife made in switzeland.

https://www.ghisler.com/deutsch.htm


Winni
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

dbannon

  • Hero Member
  • *****
  • Posts: 2792
    • tomboy-ng, a rewrite of the classic Tomboy
Re: CopyFile does not copy the file
« Reply #10 on: February 17, 2020, 09:46:43 am »
Birger52, I, or more correctly, an end user of mine, is having a similar problem.

See https://forum.lazarus.freepascal.org/index.php/topic,48284.msg348038.html#msg348038

I cannot replicate this persons problem but he sounds pretty careful and has been very helpful with feed back. What we see is DeleteFileUTF8() tells us it failed to delete a file. In fact, it turns out, I think, that it does delete the file but reports back it did not.  I have followed the calls, its really passed straight through to the Windows API. And the return value the same.

One thing I have found out is he used VeraCrypt, not on the drive my app is saving to but its loaded in memory.  So, question, are you using any sort of file or disk encryption ?

Davo

(Too mature to join in the Windows kicking. But the stories I could tell.....)
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: CopyFile does not copy the file
« Reply #11 on: February 17, 2020, 05:15:16 pm »
I used to have de same problem. This is how it happened:
   1- CopyFile to backup location
   2- Clean or regenerate the file to add new data
   3- Sometimes the PC was shutdown until the next day
Result: some times the backup file/s where missing

Never could find the problem source and I Blamed the OS file cache.

Changed the CopyFile for Rename/Move and the problem disappeared ...

cdbc

  • Hero Member
  • *****
  • Posts: 1078
    • http://www.cdbc.dk
Re: CopyFile does not copy the file
« Reply #12 on: February 17, 2020, 07:10:00 pm »
Hej Birger

Her er lige en måde at gøre det på  :)

Code: Pascal  [Select][+][-]
  1. procedure THKCollection.BackupDb; { ok }
  2. var
  3.   BackupFilename: string;
  4.   Buffer: array[0..4095] of byte; { 4 Kb buffer }
  5.   InStream,OutStream: TFileStream;
  6.   Cnt,I,Res: Int64;
  7. begin
  8.   BackupFilename:= AppSettings.BackupPath;
  9.   FillChar(Buffer,4096,0); { 11.05.2015 bc }
  10.   if BackupFilename <> 'Not defined' then begin
  11.     { now construct the actual backupname with a date and .bak extension }
  12.     BackupFilename:= ExtractFilePath(AppSettings.BackupPath)+ExtractFileName(AppSettings.Databasename); ;
  13.     system.insert('_',BackupFilename,length(BackupFilename)-3); // +_
  14.     system.insert(bcDateToStr(now),BackupFilename,length(BackupFilename)-3); // +19.04.2015
  15.     BackupFilename:= ChangeFileExt(BackupFilename,'.bak');  // *.bak
  16.     if FileExists(BackupFilename) then DeleteFile(BackupFilename); { 09.05.2015 bc }
  17.     if fDb.Connected then fDb.DisConnect; { sanity check }
  18.     InStream:= TFileStream.Create(AppSettings.Databasename,fmOpenRead);
  19.     try
  20.       InStream.Seek(0,fsFromBeginning);
  21.       OutStream:= TFileStream.Create(BackupFilename,fmCreate);
  22.       try
  23.         OutStream.Seek(0,fsFromBeginning);
  24.         Cnt:= InStream.Size; Res:= 0;
  25.         { implemented by hand }
  26.         while Cnt > 0 do begin
  27.           if Cnt > 4096 then I:= 4096 else I:= Cnt;
  28.           InStream.ReadBuffer(Buffer,I);
  29.           OutStream.WriteBuffer(Buffer,I);
  30.           dec(Cnt,I);
  31.           inc(Res,I);
  32.         end;
  33.         if Res <> InStream.Size then raise Exception.Create('Backup failed! Db-file and backup-file differs in size!');
  34.         { implemented in TStream, uses much bigger buffer }
  35. //        OutStream.CopyFrom(InStream,InStream.Size);
  36.       finally
  37.         FreeAndNil(OutStream);
  38.       end;
  39.     finally
  40.       FreeAndNil(InStream);
  41.     end;
  42.   end;
  43. end;
  44.  
Håber du kan få et par ideer  ;)
Ellers så bare spørg.
mvh. Benny ~ cdbc
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: CopyFile does not copy the file
« Reply #13 on: February 17, 2020, 07:20:39 pm »
Engelsk venligst

ezlage

  • Guest
Re: CopyFile does not copy the file
« Reply #14 on: February 17, 2020, 08:12:13 pm »
I have this:
Code: Pascal  [Select][+][-]
  1. procedure TMyStripForm.BackupFile(aFile:string); // Laver backup af databasen
  2. var
  3.   idx : integer;
  4.   oName, bName : string;
  5. begin
  6.   for idx := 2 downto 0 do begin
  7.     oName := aFile;
  8.     if idx > 0 then oName += '.backup_'+IntToStr(idx);
  9.     if FileExists(oName) then begin
  10.       bName := aFile+'.backup_'+IntToStr(idx+1);
  11.       CopyFile(oName, bName, [cffPreserveTime, cffOverwriteFile]);
  12.     end;
  13.   end;
  14. end;

And it has been functioning.
So - needed to use he backup.
Deleted original and renamed .backup_1 to original.
Kind of the whole idea.
and it also functioned.
With the exception, that the original is now no longer copied to .backup_1

from
https://wiki.freepascal.org/CopyFile
Code: Pascal  [Select][+][-]
  1. type
  2.  TCopyFileFlag = (
  3.    cffOverwriteFile,
  4.    cffCreateDestDirectory,
  5.    cffPreserveTime
  6.    );
  7.  TCopyFileFlags = set of TCopyFileFlag;
  8.  
  9. function CopyFile(const SrcFilename, DestFilename: string): boolean;
  10. function CopyFile(const SrcFilename, DestFilename: string; PreserveTime: boolean): boolean;
  11. function CopyFile(const SrcFilename, DestFilename: string; Flags: TCopyFileFlags=[cffOverwriteFile]): boolean;

Anybody has a workaround - or an explanation for that matter?

I tested your code here and I didn't found any problem. Everything worked as expected. Try to do a "Cleanup and build", check if there is some external program locking the file after you rename it (antivirus, indexer or something like that). Check also if there is some special character or custom permissions at the file or directory.
« Last Edit: February 17, 2020, 10:13:38 pm by ezlage »

 

TinyPortal © 2005-2018