como faço p/ verificar se o arquivo existe no diretorio

Delphi

23/05/2003

Ola p/ quem puder me ajudar !

AssignFile(TXT,´C:\USIProjetos\Prods.TXT´) ;

Associei a variavel TXT ao arquivo Prods.TXT que esta no diretorio
C:\USIProjetos

Pergunta: como faço p/ verificar se o arquivo existe no diretorio ? Se não existir ele retornaria uma mensagem dizendo que não existe.

Obrigado
Edmar


Martelato

Martelato

Curtidas 0

Respostas

Jairroberto

Jairroberto

23/05/2003

Olá, Edmar!

Você deve fazer esta verificação antes mesmo de usar o AssignFile. Há uma função no Delphi para isso:

function FileExists(const FileName: string): Boolean;


Ela está declarada na unit SysUtils. Você pode usá-la da seguinte forma:

procedure TForm1.Button1Click(Sender: TObject);
var
  NomeArquivo: string;
  TXT: TextFile;
begin
  NomeArquivo := ´C:\USIProjetos\Prods.TXT´;
  if not FileExists(NomeArquivo) then
    raise Exception.CreateFmt(´O arquivo "¬s" não existe!´, [NomeArquivo]);

  AssignFile(TXT, NomeArquivo);
  Reset(TXT); // abre o arquivo
  try
    // processe o arquivo como desejar
  finally
    CloseFile(TXT); // fecha o arquivo
  end;
end;



Um abraço,
Jair


GOSTEI 0
POSTAR