home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 August / VPR9708A.ISO / D3TRIAL / INSTALL / DATA.Z / FMXUTILS.PAS < prev    next >
Pascal/Delphi Source File  |  1997-05-08  |  4KB  |  124 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: TFileName);
  28. var
  29.   CopyBuffer: Pointer; { buffer for copying }
  30.   BytesCopied: Longint;
  31.   Source, Dest: Integer; { handles }
  32.   Destination: TFileName; { holder for expanded destination name }
  33. const
  34.   ChunkSize: Longint = 8192; { copy in 8K chunks }
  35. begin
  36.   Destination := ExpandFileName(DestName); { expand the destination path }
  37.   if HasAttr(Destination, faDirectory) then { if destination is a directory... }
  38.     Destination := Destination + '\' + ExtractFileName(FileName); { ...clone file name }
  39.   GetMem(CopyBuffer, ChunkSize); { allocate the buffer }
  40.   try
  41.     Source := FileOpen(FileName, fmShareDenyWrite); { open source file }
  42.     if Source < 0 then raise EFOpenError.CreateFmt(SFOpenError, [FileName]);
  43.     try
  44.       Dest := FileCreate(Destination); { create output file; overwrite existing }
  45.       if Dest < 0 then raise EFCreateError.CreateFmt(SFCreateError, [Destination]);
  46.       try
  47.         repeat
  48.           BytesCopied := FileRead(Source, CopyBuffer^, ChunkSize); { read chunk }
  49.           if BytesCopied > 0 then { if we read anything... }
  50.             FileWrite(Dest, CopyBuffer^, BytesCopied); { ...write chunk }
  51.         until BytesCopied < ChunkSize; { until we run out of chunks }
  52.       finally
  53.         FileClose(Dest); { close the destination file }
  54.       end;
  55.     finally
  56.       FileClose(Source); { close the source file }
  57.     end;
  58.   finally
  59.     FreeMem(CopyBuffer, ChunkSize); { free the buffer }
  60.   end;
  61. end;
  62.  
  63.  
  64. { MoveFile procedure }
  65. {
  66.   Moves the file passed in FileName to the directory specified in DestDir.
  67.   Tries to just rename the file.  If that fails, try to copy the file and
  68.   delete the original.
  69.  
  70.   Raises an exception if the source file is read-only, and therefore cannot
  71.   be deleted/moved.
  72. }
  73.  
  74. procedure MoveFile(const FileName, DestName: string);
  75. var
  76.   Destination: string;
  77. begin
  78.   Destination := ExpandFileName(DestName); { expand the destination path }
  79.   if not RenameFile(FileName, Destination) then { try just renaming }
  80.   begin
  81.     if HasAttr(FileName, faReadOnly) then  { if it's read-only... }
  82.       raise EFCantMove.Create(Format(SFCantMove, [FileName])); { we wouldn't be able to delete it }
  83.       CopyFile(FileName, Destination); { copy it over to destination...}
  84. //      DeleteFile(FileName); { ...and delete the original }
  85.   end;
  86. end;
  87.  
  88. { GetFileSize function }
  89. {
  90.   Returns the size of the named file without opening the file.  If the file
  91.   doesn't exist, returns -1.
  92. }
  93.  
  94. function GetFileSize(const FileName: string): LongInt;
  95. var
  96.   SearchRec: TSearchRec;
  97. begin
  98.   if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then
  99.     Result := SearchRec.Size
  100.   else Result := -1;
  101. end;
  102.  
  103. function FileDateTime(const FileName: string): System.TDateTime;
  104. begin
  105.   Result := FileDateToDateTime(FileAge(FileName));
  106. end;
  107.  
  108. function HasAttr(const FileName: string; Attr: Word): Boolean;
  109. begin
  110.   Result := (FileGetAttr(FileName) and Attr) = Attr;
  111. end;
  112.  
  113. function ExecuteFile(const FileName, Params, DefaultDir: string;
  114.   ShowCmd: Integer): THandle;
  115. var
  116.   zFileName, zParams, zDir: array[0..79] of Char;
  117. begin
  118.   Result := ShellExecute(Application.MainForm.Handle, nil,
  119.     StrPCopy(zFileName, FileName), StrPCopy(zParams, Params),
  120.     StrPCopy(zDir, DefaultDir), ShowCmd);
  121. end;
  122.  
  123. end.
  124.