Archive

Posts Tagged ‘files’

How to Copy / Move Files

December 15th, 1999 m3Rlin 1 comment

Delphi’s runtime library does not provide any routines for copying or moving files. However, you can directly call the Windows API CopyFile() function to copy a file. CopyFile() is also useful when moving files across drives because neither Delphi’s RenameFile() function nor the Windows API MoveFile() function can rename/move files across drives.

!!Note: The file attributes for the existing file are copied to the new file, but the security attributes are not.

Here’s the Delphi code:

uses
  Windows;
...
  (* sSource - (PChar or string if typecasted) The full name of the source
  **   file.
  ** sDestination - (PChar or string if typecasted) The full name of the
  **   destination file.
  ** bNoOverwrite - (Boolean) Set False if you want to overwrite existing
  **   files, else set True.
  **
  ** Function result: True if successful, else it will return False.
  *)
  CopyFile(PChar(sSource), PChar(sDestination), bNoOverwrite);
...

Examples:

{ Both examples allow file overwriting }
CopyFile('C:\\Autoexec.bat', 'A:\\Backup\\Autoexec.bat', False);
CopyFile(PChar(Edit1.Text), PChar(Edit2.Text), False);

How to Print Text and Text Files in Delphi

December 15th, 1999 m3Rlin No comments

This code how to print text and text files. This is the simplest way to print without using any external functions. All you have to do is open the LPT1 port (this is the default printer port) as if it where a file.

This code can be easily ported to Turbo Pascal (just change the function and variable type names).

var
  fPrinter: TextFile;
begin
  (* Write your printing code here. For files you may want to use a "for"
  ** or "while not Eof(fPrinter)" statement.
  **
  ** Example: WriteLn(fPrinter, 'The text you want to print.');
  *)
  AssignFile(fPrinter, 'LPT1');
  Rewrite(fPrinter);
  CloseFile(fPrinter);
end;