Page 1 of 1

Try to create non fragmented files on copy/move

Posted: 2007-11-04, 15:06 UTC
by maxB
Hi Christian,

I noticed that TC creates fragmented files when copying or moving files.
In the article "Fix verteilt" from the heise magazin "c't" 21/2005 about fragmentation they suggested to use the Windows-API-Functions SetFilePointer() and SetEndOfFile() like in this Delphi 7 sample:
(only tested on NTFS from Win XP)

Code: Select all

var dstHandle: Integer;
    srcname, dstname: string;
    filesize: Int64;
    li: LARGE_INTEGER;
    success: longbool;
    sr   : TSearchRec;
begin
  // get size of source file
  srcname:='c:\1.bin';
  if FindFirst(srcname, faAnyFile, sr)=0 then
      filesize:=Int64(sr.FindData.nFileSizeHigh * 4294967295) + Int64(sr.FindData.nFileSizeLow);
  FindClose(sr);

  // open destination file
  dstname:='c:\2.bin';
  dstHandle:=FileCreate(dstname);

  li.QuadPart:=filesize;

  // set filepointer to size of source file
  if (SetFilePointer(Cardinal(dstHandle), li.LowPart, @li.HighPart, FILE_BEGIN) <> -1) then
      // set the end of the destination file so that the
      // filesystem knows how long the file will be
      success:=SetEndOfFile(Cardinal(dstHandle))
  else
      success:=FALSE;

  // set back the filepointer to start of destination file
  SetFilePointer(Cardinal(dstHandle), 0, nil, FILE_BEGIN);

  // file-operation...

  FileClose(dstHandle);
end;
If there is enough free space on disk, this will work in most cases. Nevermind if not, the handle is always there.

Maybe in future versions...

Greets and thanks for this great piece of software.

maxB

Posted: 2007-11-04, 15:36 UTC
by gigaman
If this really works (didn't try), I'd say it's a good idea.

Posted: 2007-11-05, 02:58 UTC
by StatusQuo
That sounds like the solution to most fragmentation problems.
Would be great, if this works.

Posted: 2007-11-05, 17:37 UTC
by ghisler(Author)
Total Commander already does EXACTLY that! I do not see any fragmentation from TC, except when the disk is already so much fragmented that the file cannot be allocated in one chunk...

Posted: 2007-11-05, 18:57 UTC
by maxB
Oh, I'm sorry, I took a look again and saw that directories were fragmented after the kopy/move. Didn't want to be precocious...so nevermind

Posted: 2007-11-05, 19:29 UTC
by StatusQuo
Nice to know that. :)

Posted: 2007-11-05, 20:13 UTC
by ghisler(Author)
Unfortunately directories cannot be preallocated like files - or does anyone know a way to do that?