home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2000 October / tst.iso / programs / borland / RUNIMAGE / DELPHI40 / DEMOS / DOC / FILMANEX / FMXUTILS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-22  |  4.2 KB  |  135 lines

  1. unit FmxUtils;
  2.  
  3. interface
  4.  
  5. uses SysUtils, Windows, Classes, Consts;
  6.  
  7. type
  8.   EInvalidDest = class(EStreamError);
  9.   EFCantMove = class(EStreamError);
  10.  
  11. procedure CopyFile(const FileName, DestName: string);
  12. procedure MoveFile(const FileName, DestName: string);
  13. function GetFileSize(const FileName: string): LongInt;
  14. function FileDateTime(const FileName: string): TDateTime;
  15. function HasAttr(const FileName: string; Attr: Word): Boolean;
  16. function ExecuteFile(const FileName, Params, DefaultDir: string;
  17.   ShowCmd: Integer): THandle;
  18.  
  19. implementation
  20.  
  21. uses Forms, ShellAPI;
  22.  
  23. const
  24.   SInvalidDest = 'Destination %s does not exist';
  25.   SFCantMove = 'Cannot move file %s';
  26.  
  27. procedure CopyFile(const FileName, DestName: string);
  28. var
  29.   CopyBuffer: Pointer; { buffer for copying }
  30.   BytesCopied: Longint;
  31.   Source, Dest: Integer; { handles }
  32.   Len: Integer;
  33.   Destination: TFileName; { holder for expanded destination name }
  34. const
  35.   ChunkSize: Longint = 8192; { copy in 8K chunks }
  36. begin
  37.   Destination := ExpandFileName(DestName); { expand the destination path }
  38.   if HasAttr(Destination, faDirectory) then { if destination is a directory... }
  39.   begin
  40.     Len :=  Length(Destination);
  41.     if Destination[Len] = '\' then
  42.       Destination := Destination + ExtractFileName(FileName) { ...clone file name }
  43.     else
  44.       Destination := Destination + '\' + ExtractFileName(FileName); { ...clone file name }
  45.   end;
  46. GetMem(CopyBuffer, ChunkSize); { allocate the buffer }
  47.   try
  48.     Source := FileOpen(FileName, fmShareDenyWrite); { open source file }
  49.     if Source < 0 then raise EFOpenError.CreateFmt(SFOpenError, [FileName]);
  50.     try
  51.       Dest := FileCreate(Destination); { create output file; overwrite existing }
  52.       if Dest < 0 then raise EFCreateError.CreateFmt(SFCreateError, [Destination]);
  53.       try
  54.         repeat
  55.           BytesCopied := FileRead(Source, CopyBuffer^, ChunkSize); { read chunk }
  56.           if BytesCopied > 0 then { if we read anything... }
  57.             FileWrite(Dest, CopyBuffer^, BytesCopied); { ...write chunk }
  58.         until BytesCopied < ChunkSize; { until we run out of chunks }
  59.       finally
  60.         FileClose(Dest); { close the destination file }
  61.       end;
  62.     finally
  63.       FileClose(Source); { close the source file }
  64.     end;
  65.   finally
  66.     FreeMem(CopyBuffer, ChunkSize); { free the buffer }
  67.   end;
  68. end;
  69.  
  70.  
  71. { MoveFile procedure }
  72. {
  73.   Moves the file passed in FileName to the directory specified in DestDir.
  74.   Tries to just rename the file.  If that fails, try to copy the file and
  75.   delete the original.
  76.  
  77.   Raises an exception if the source file is read-only, and therefore cannot
  78.   be deleted/moved.
  79. }
  80.  
  81. procedure MoveFile(const FileName, DestName: string);
  82. var
  83.   Destination: string;
  84. begin
  85.   Destination := ExpandFileName(DestName); { expand the destination path }
  86.   if not RenameFile(FileName, Destination) then { try just renaming }
  87.   begin
  88.     if HasAttr(FileName, faReadOnly) then  { if it's read-only... }
  89.       raise EFCantMove.Create(Format(SFCantMove, [FileName])); { we wouldn't be able to delete it }
  90.       CopyFile(FileName, Destination); { copy it over to destination...}
  91. //      DeleteFile(FileName); { ...and delete the original }
  92.   end;
  93. end;
  94.  
  95. { GetFileSize function }
  96. {
  97.   Returns the size of the named file without opening the file.  If the file
  98.   doesn't exist, returns -1.
  99. }
  100.  
  101. function GetFileSize(const FileName: string): LongInt;
  102. var
  103.   SearchRec: TSearchRec;
  104. begin
  105.   if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then
  106.     Result := SearchRec.Size
  107.   else Result := -1;
  108. end;
  109.  
  110. function FileDateTime(const FileName: string): System.TDateTime;
  111. begin
  112.   Result := FileDateToDateTime(FileAge(FileName));
  113. end;
  114.  
  115. function HasAttr(const FileName: string; Attr: Word): Boolean;
  116. var
  117.  FileAttr: Integer;
  118. begin
  119.   FileAttr := FileGetAttr(FileName);
  120.   if FileAttr = -1 then FileAttr := 0;
  121.   Result := (FileAttr and Attr) = Attr;
  122. end;
  123.  
  124. function ExecuteFile(const FileName, Params, DefaultDir: string;
  125.   ShowCmd: Integer): THandle;
  126. var
  127.   zFileName, zParams, zDir: array[0..79] of Char;
  128. begin
  129.   Result := ShellExecute(Application.MainForm.Handle, nil,
  130.     StrPCopy(zFileName, FileName), StrPCopy(zParams, Params),
  131.     StrPCopy(zDir, DefaultDir), ShowCmd);
  132. end;
  133.  
  134. end.
  135.