Files & folders

Delete all files within directory

Posted in Files & folders

Use FileListBox component. Specify a necessary directory to this component and remove files by using DeleteFile procedure.

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  for i:=0 to FileListBox1.Items.Count-1 do
    DeleteFile(FileListBox1.Items[i]);
  FileListBox1.Update;
end;

Check if a file exists

Posted in Files & folders

Use FileExists function. If file exists, then this function will return True.

procedure TForm1.Button1Click(Sender: TObject);
begin
  if FileExists(Edit1.Text) then
    ShowMessage('File already exists!')
  else
    ShowMessage('This file is unique');
end;

For more 

Recognize CD volume label

Posted in Files & folders

This task is solved in two steps. 1) definition of the letter of the existing CD-ROM drive. In these purposes GetLogicalDrives and GetDriveType functions are used. 2) obtaining of the information on the device by calling the GetVolumeInformation function. This unit shows it:

 public
    { Public declarations }
    function GetCDLetter: string;
    function GetCDLabel(Drive: string): string;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption:=GetCDLabel(GetCDLetter());
end;

function TForm1.GetCDLetter: string;
var
  N: Byte;
  Drv: string;
  Drives: set of 0..25;
begin
  Integer(Drives):=GetLogicalDrives;
  for N:=0 to 25 do
    if N in Drives then
    begin
      Drv:=Char(N+Ord('A'))+':\';
      if(GetDriveType(PChar(Drv))=5) then
      begin
        Result:=Drv;
        Exit;
      end;
    end;
end;

function TForm1.GetCDLabel(Drive: string): string;
var
  VolumeName: array[0..255] of Char;
  FileSystemType: array[0..255] of Char;
  SerialNum: DWORD;
  MaxFilenameLength: DWORD;
  Flags: DWORD;
begin
  if (GetVolumeInformation(
    PChar(Drive),
    VolumeName,
    256,
    @SerialNum,
    MaxFilenameLength,
    Flags,
    FileSystemType,
    256)) then Result:=VolumeName
  else Result:='No CD Present';
end;

Show list of valid drives

Posted in Files & folders

Use GetLogicalDriveStrings function for solving of this problem. This function returns point to a string, which has such representing: c:\#0d:\#0#0.

procedure TForm1.Button1Click(Sender: TObject);
var
  MyStr: PChar;
  i, Length: Integer;
const
  Size: Integer = 200;
begin
  GetMem(MyStr, Size);
  Length:=GetLogicalDriveStrings(Size, MyStr);
  for i:=0 to Length-1 do
  begin
    if (MyStr[i]>='a')and(MyStr[i]<='z') then
      Memo1.Lines.Add(MyStr[i]+':\');
  end;
  FreeMem(MyStr);
end;

Recognize CDROM drive

Posted in Files & folders

Use GetDriveType-function for a type definition of the device. Returns values: 0 - Unknown 1 - Unknown 2 - Floppy Disk 3 - Local Drive 4 - Network Drive 5 - CD-Rom 6 - RAM-Disk implementation Uses ShellApi;

procedure TForm1.Button1Click(Sender: TObject);
var
  N: Byte;
  Drv: string;
  Drives: set of 0..25;
begin
  Integer(Drives):=GetLogicalDrives;
  for N:=0 to 25 do
    if N in Drives then
    begin
      Drv:=Char(N+Ord('A'))+':';
      if(GetDriveType(PChar(Drv))=5) then
        Label1.Caption:='Drive '+Drv+' is the CD-Rom Drive';
    end;
end;