Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filename from File handle

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,312
US
I got a question I've been struggling to seek an answer on. Is there a way to get a filename out of the system for an open file handle? Say something like:

Code:
program test;
  var
    f: file;
    filename: string;
  begin
    assign(f, 'test.txt');
    reset(f);
    filename := GetFileNameFromAttr(f);
    writeln(filename);
  end.

Filename should be 'test.txt'.

Can this be done?
 
Not with an untyped file. It will work with textfile:
Code:
var
  f: [b][red]textfile[/red][/b];
  filename: string;
begin
  assign(f, 'c:\test.txt');
  reset(f);
  filename:= [red]TTextRec(f).Name[/red];
  writeln(filename);
  close(f);
end.

Roo
Delphi Rules!
 
Unfortunately, I need for it to be able to work with any defined file handle (text, file of, file). Sorry.
 
Okay, I got it. I wanted to typecast the file variable to TFileRec to get what I wanted. Seems to work with any file type I feed it. Thanks for the pointer in the right direction to look.
 
Can you post your solution? I looked at TFileRec last night and did not see how you could us it.

Roo
Delphi Rules!
 
Code:
procedure SetFAttr(var F; Attr: Word);
  begin
    doserror := 0;
    doserror := FileSetAttr(TFileRec(f).Name, attr);
  end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top