CopyFile does not copy the file lazarus / 61-Beginners ---------------------------------------------------------------------- Birger52 · 2020-02-13 11:01 I have this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMyStripForm.BackupFile(aFile:string); // Laver backup af databasenvar  idx : integer;  oName, bName : string;begin  for idx := 2 downto 0 do begin    oName := aFile;    if idx > 0 then oName += '.backup_'+IntToStr(idx);    if FileExists(oName) then begin      bName := aFile+'.backup_'+IntToStr(idx+1);      CopyFile(oName, bName, [cffPreserveTime, cffOverwriteFile]);    end;  end;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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type TCopyFileFlag = (   cffOverwriteFile,   cffCreateDestDirectory,   cffPreserveTime   ); TCopyFileFlags = set of TCopyFileFlag; function CopyFile(const SrcFilename, DestFilename: string): boolean;function CopyFile(const SrcFilename, DestFilename: string; PreserveTime: boolean): boolean;function CopyFile(const SrcFilename, DestFilename: string; Flags: TCopyFileFlags=[cffOverwriteFile]): boolean; Anybody has a workaround - or an explanation for that matter? ---------------------------------------------------------------------- Birger52 · 2020-02-13 11:06 Tried altering the code a bit.. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       if not CopyFile(oName, bName, [cffPreserveTime, cffOverwriteFile]) then      Msg.DispMsg('Error backing up database'+sLineBreak+oName, mtErr);  Which reveals, that CopyFile does not only not copy the file - it also returns true, as if the file was actually copied... ---------------------------------------------------------------------- shadow · 2020-02-13 12:18 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 · 2020-02-13 14:09 For debugging purposes, try with this to see what it's doing: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i           if CopyFile(oName, bName, [cffPreserveTime, cffOverwriteFile]) then          ShowMessageFmt('Backed %s to %s',[oName, bName])          else            Msg.DispMsg('Error backing up database'+sLineBreak+oName, mtErr); ---------------------------------------------------------------------- wp · 2020-02-13 14:30 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 · 2020-02-13 14:57 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 · 2020-02-14 10:17 Quote from: winni on February 13, 2020, 03:57:56 pmOne 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..... ---------------------------------------------------------------------- winni · 2020-02-14 11:00 Quote from: Zvoni on February 14, 2020, 11:17:06 amQuote from: winni on February 13, 2020, 03:57:56 pmOne 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 · 2020-02-14 11:16 Quote from: winni on February 14, 2020, 12:00:58 pmQuote from: Zvoni on February 14, 2020, 11:17:06 amQuote from: winni on February 13, 2020, 03:57:56 pmOne 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....... ---------------------------------------------------------------------- JLWest · 2020-02-17 08:18 Thanks for tip on Total Commander. A lot better than Exployer. Quote from: winni on February 13, 2020, 03:57:56 pmOne 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 ---------------------------------------------------------------------- dbannon · 2020-02-17 08:46 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.msg3480 38.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.....) ---------------------------------------------------------------------- garlar27 · 2020-02-17 16:15 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 · 2020-02-17 18:10 Hej Birger Her er lige en måde at gøre det på  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure THKCollection.BackupDb; { ok }var  BackupFilename: string;  Buffer: array[0..4095] of byte; { 4 Kb buffer }  InStream,OutStream: TFileStream;  Cnt,I,Res: Int64;begin  BackupFilename:= AppSettings.BackupPath;  FillChar(Buffer,4096,0); { 11.05.2015 bc }  if BackupFilename <> 'Not defined' then begin    { now construct the actual backupname with a date and .bak extension }    BackupFilename:= ExtractFilePath(AppSettings.BackupPath)+ExtractFileName(AppSettings.Da tabasename); ;    system.insert('_',BackupFilename,length(BackupFilename)-3); // +_    s ystem.insert(bcDateToStr(now),BackupFilename,length(BackupFilename)- 3); // +19.04.2015    BackupFilename:= ChangeFileExt(BackupFilename,'.bak');  // *.bak    if FileExists(BackupFilename) then DeleteFile(BackupFilename); { 09.05.2015 bc }    if fDb.Connected then fDb.DisConnect; { sanity check }    InStream:= TFileStream.Create(AppSettings.Databasename,fmOpenRead);    try      InStream.Seek(0,fsFromBeginning);      OutStream:= TFileStream.Create(BackupFilename,fmCreate);      try        OutStream.Seek(0,fsFromBeginning);        Cnt:= InStream.Size; Res:= 0;        { implemented by hand }        while Cnt > 0 do begin        if Cnt > 4096 then I:= 4096 else I:= Cnt;          InStream.ReadBuffer(Buffer,I);          OutStream.WriteBuffer(Buffer,I);          dec(Cnt,I);          inc(Res,I);        end;        if Res <> InStream.Size then raise Exception.Create('Backup failed! Db-file and backup-file differs in size!');        { implemented in TStream, uses much bigger buffer }//      OutStream.CopyFrom(InStream,InStream.Size);      finally        FreeAndNil(OutStream);      end;    finally      FreeAndNil(InStream);    end;  end;end; Håber du kan få et par ideer  Ellers så bare spørg. mvh. Benny ~ cdbc ---------------------------------------------------------------------- winni · 2020-02-17 18:20 Engelsk venligst ---------------------------------------------------------------------- ezlage · 2020-02-17 19:12 Quote from: Birger52 on February 13, 2020, 12:01:32 pmI have this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMyStripForm.BackupFile(aFile:string); // Laver backup af databasenvar  idx : integer;  oName, bName : string;begin  for idx := 2 downto 0 do begin    oName := aFile;    if idx > 0 then oName += '.backup_'+IntToStr(idx);    if FileExists(oName) then begin      bName := aFile+'.backup_'+IntToStr(idx+1);      CopyFile(oName, bName, [cffPreserveTime, cffOverwriteFile]);    end;  end;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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type TCopyFileFlag = (   cffOverwriteFile,   cffCreateDestDirectory,   cffPreserveTime   ); TCopyFileFlags = set of TCopyFileFlag; function CopyFile(const SrcFilename, DestFilename: string): boolean;function CopyFile(const SrcFilename, DestFilename: string; PreserveTime: boolean): boolean;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. ---------------------------------------------------------------------- dbannon · 2020-02-17 23:03 Because I wanted to be able to check on how its happening, I split the CopyFile() into a delete/copy process on Windows, something like this - Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    // First write the required file using Stream.SaveToFile(TempName), Stream.Free, no errors reported.        if not DeleteFile(FileName) then begin        ErrorMsg := SysErrorMessage(GetLastOSError);        Debugln('Failed using DeleteFileUTF8 - file name is :' + Filename);      Debugln('OS Error Msg : ' + ErrorMsg);        if not FileExistsUTF8(FileName) then            debugln('But, FileExists says its gone, proceed !');        else begin {do other serious stuff} end;   end;   result := CopyFile(TempName, FileName); The person experiencing this issue has sent me several logs, in all cases the even though DeleteFile has returned false and the OS error message is "The file can not be found" (in Swedish) the FileExists() says its gone and we proceed to copy over it (or copy to where it was). However, while this seemed to work, he is now reporting problems that sound suspiciously like a shortage of resource issues, maybe file handles ? So, does sound like a serious problem. But I cannot see his system. I believe I have a number of Windows users and only one is seeing this problem. A hard one .... Davo ---------------------------------------------------------------------- winni · 2020-02-17 23:25 Hi! Okay, stumbling in the fog, but I would try this: If not deleteFile ...     Sleep  (200) or 300 --- perhaps the encryption/decryption needs time??    And now the old game with findfirst/findnext/findclose - let you show the whole directory.    Does the file still exist??   The has the side effect that the file pointer is heavily moved - if it is a problem with it.   Which fs? Is it NTFS? Winni ---------------------------------------------------------------------- winni · 2020-02-17 23:39 Hi! Another idea : Do you just detect just a bug in the M$ file cache? They do that since some versions. How are the two flags FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH set in the windows version? And have a look at https://docs.microsoft.com/en-us/windows/win32/fileio/file-caching Ugly problem ! Winni ---------------------------------------------------------------------- jamie · 2020-02-18 00:49 I don't know why you guys don't use the ShFileOperation API call ? It allows you just about all you need including a user report so they don't get board while waiting..  I will tell you this however, files that are currently opened by some process will not get deleted but will be flagged so that the next time the OS views the files or starts up they are remembered along with the operation and once the files become closed and not locked the shell processes them.  That is, if you are using the Shell functions.. At least that has been my experience.. ---------------------------------------------------------------------- dbannon · 2020-02-18 00:50 Ugly indeed. Obviously, the Linux file system is massively cached but thats really not the user's business except during a power down event.  And there we have the sync command. That write up does not mention a standalone command/api call, just parameters to CreateFile. So, difficult to use with copyfile() or deletefile().  StackOverflow has quite a lot of (negative) discussion. I suspect what we are seeing is a conflict between Windows read-cache and write-caches.  One file has been read, is therefore in the read cache, its then either deleted or overwritten via the write-cache and suddenly, the write-cache does not agree with the read-cache. A possible solution is to abandon the (very useful) process of writing the file out as a tmp file and then moving it onto the real file. While very good practice from a data safety principle, it confuses Window's cache. Yet, its on NTFS that the process is particularly valuable. ext3 and ext4 are well journaled. Sigh .... ---------------------------------------------------------------------- winni · 2020-02-18 01:10 Hi! Can't  believe it! I was seraching for the windows counterpart of sync. It exists! Have a look at: https://docs.microsoft.com/en-us/sysinternals/downloads/sync Perhaps that helps!? Winni ---------------------------------------------------------------------- 440bx · 2020-02-18 01:12 Quote from: dbannon on February 18, 2020, 12:03:15 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    // First write the required file using Stream.SaveToFile(TempName), Stream.Free, no errors reported.        if not DeleteFile(FileName) then begin... ... he is now reporting problems that sound suspiciously like a shortage of resource issues, maybe file handles ? Here is a guess based on the comment in your code and, the fact that there is an apparent shortage of resources:  you use "Stream.SaveToFile(TempName)", the first thing I would do is to ensure the file handle used by that stream function has been closed, because if it hasn't and an attempt to delete the file is made, the behavior will depend on how the O/S will view the attempt to delete an open file. Windows, depending on the situation, could mark the file as "delete on close" and report the file as not existing until it is closed.  That has the potential of accumulated file handles in the system. HTH. ---------------------------------------------------------------------- dbannon · 2020-02-18 01:54 Quote from: jamie on February 18, 2020, 01:49:56 amI don't know why you guys don't use the ShFileOperation API call ? Hmm, "This function has been replaced in Windows Vista by IFileOperation." - is that still a useful thing in Windows 10 ?  Quote from: jamie on February 18, 2020, 01:49:56 am I will tell you this however, files that are currently opened by some process will not get deleted but will be flagged so that the next time the OS views the files or starts up they are remembered along with the operation and once the files become closed and not locked the shell processes them.  That is, if you are using the Shell functions.. Well, I am pretty sure in my case the files are not open, they are opened for reading a little while before this problem but are closed.  And the issue is not so much that the functions don't work, its that the return code is sometimes incorrect. Jamie, I would love to see a copy n paste code snip-it of that ShFileOperation in pascal ? Davo ---------------------------------------------------------------------- dbannon · 2020-02-18 02:18 Quote from: 440bx on February 18, 2020, 02:12:45 amHere is a guess based on the comment in your code and, the fact that there is an apparent shortage of resources:  you use "Stream.SaveToFile(TempName)", the first thing I would do is to ensure the file handle used by that stream function has been closed..... What you say makes sense 440bx, but in my case, the reported fail to delete is of a file that has not been written to, was recently read and definitly closed at least several seconds before the attempt to delete. The file being written to is called TempName and its written then moved to where the deleted file was. winni, good find but requires admin rights.  No good on an app I distribute.  Wonder why, its always safe to let a user run sync on unix .... Oh, and winni, I do have code in there that retries after a delay (in {do other serious stuff} in my example above) but so far, its never been executed. We find that the first "if FileExits() says that it does not so we proceed. Davo ---------------------------------------------------------------------- 440bx · 2020-02-18 03:28 Quote from: dbannon on February 18, 2020, 03:18:38 am... definitly closed at least several seconds before the attempt to delete. The file being written to is called TempName and its written then moved to where the deleted file was. At this time, I can't think of another reason for the problem.  Are the logs you got from the user something you can share ? there might be a clue in there. ---------------------------------------------------------------------- dbannon · 2020-02-18 04:16 Quote from: 440bx on February 18, 2020, 04:28:08 amAt this time, I can't think of another reason for the problem.  Two aspects, first is he is using this File Encryption tool, VeraCrypt but googling for that does not indicate its a problem. Secondly, is winni's suggestion about Window's caching. It does sound like a badly managed cache to me.  Quote from: 440bx on February 18, 2020, 04:28:08 amAre the logs you got from the user something you can share ? there might be a clue in there. Nothing interesting - Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i .....Failed using DeleteFileUTF8 - file name is :C:\Users\UUUUU.DESKTOP- ZZZZZZZ\AppData\Roaming\tomboy- ng\notes\9700D8CD-826C-409D-B180-77EF97F9DF03.noteOS: Det går inte att hitta filen.But, FileExists says its gone, proceed ! generated from #695 of https://github.com/tomboy-notes/tomboy- ng/blob/master/tomboy-ng/savenote.pas The swedish OS error msg translates to "cannot find file", I have replaced the start of the user's user name with 'UUUUU'. David ---------------------------------------------------------------------- 440bx · 2020-02-18 04:38 I don't have anything of value to offer but, an opinion... Quote from: dbannon on February 18, 2020, 05:16:51 amIt does sound like a badly managed cache to me.  I suppose that is possible but, it seems that if that were the case, that user and just about every Windows user would be experiencing some problems.  That said, maybe the user's file encryption software combined with something else he is using (e.g, real time backup software) might be the cause.  Obviously, all speculation and not even useful. Quote from: dbannon on February 18, 2020, 05:16:51 amNothing interesting - I agree, not exactly illuminating.  Here is a shot in the dark, instead of using delete/copy, try using "MoveFile" if you can, just to see what that API says. ---------------------------------------------------------------------- Zvoni · 2020-02-18 07:54 Quote from: dbannon on February 18, 2020, 02:54:50 amJamie, I would love to see a copy n paste code snip-it of that ShFileOperation in pascal ? Davo How good's your german? Note: It's "explicitly" for Delphi, and it's from year 2002 https://www.delphipraxis.net/1260-dateioperationen-mit- shfileoperation.html The first one uses TStrings, the second one plain strings as FileNames You need Unit "ShellApi" ---------------------------------------------------------------------- Birger52 · 2020-02-24 00:26 Took me a while. Sorry about that. Problem was, that there was a misspelling of the filename, when calling the procedure. The procedure is actually functioning just fine.... I read all of your ideas, comments and discussions. Interesting and many idea's. I use a plain Win7 - with no compression or anything else special running. Love TC - been using it since Windoze98... A lot of discussions on deleting files. I just ran into something.... I have SysUtils in the units uses and FileUtil in the implementations uses. I have a DeleteFile(Filename) where Filename is a  UnicodeString (defined simply as Filename:string;) Compiling I get: Error: Incompatible type for arg no. 1: Got "AnsiString", expected "PChar" Using the newly learned trick with [Alt]+[up] I jump to the definition of DeleteFile. It is in fileutilh.inc line 155: Function DeleteFile (Const FileName : RawByteString) : Boolean; In the same file line 135 is another (20 lines up and even marked...): Function DeleteFile (Const FileName : UnicodeString) : Boolean; Why does the compiler choose the overwritten function that has the wrong parameter type over the one with the right type? Looking up the implementation - [Ctrl][Shift][Up] - makes it even more obscure. The implemtation the compiler chooses to use looks like this: Function DeleteFile (Const FileName : RawByteString) : Boolean; begin   Result:=DeleteFile(UnicodeString(FileName)); end; Which is basically a call to the one it should have chosen in the first place.... ( The other one implements the same as the one in SysUtil: Function DeleteFile (Const FileName : UnicodeString) : Boolean; begin   Result:=Windows.DeleteFileW(PWidechar(FileName)); end; ) Using SysUtils.DeleteFile() compiles fine.