Link to home
Start Free TrialLog in
Avatar of pede
pede

asked on

Filesize of textfiles

Hi,

How can I get the size of a textfile? Filesize() does not work on textfiles.

/Pede

Avatar of RBertora
RBertora
Flag of United Kingdom of Great Britain and Northern Ireland image

Have you tried

DWORD GetFileSize(

    HANDLE  hFile,      // handle of file to get size of
    LPDWORD  lpFileSizeHigh,       // address of high-order word for file size
   );

Rob ;-)
Avatar of rwilson032697
rwilson032697

You can do this:

var
  f : file of byte;

....
assignfile(f, 'text.txt');
size := filesize(f);
....

Cheers,

Raymond.
Hi pede,

just use either:

OpenFile + GetFileSize

or create a file stream and query its Size property.

Ciao, Mike
Wow, I thought, I were fast...
function GetFileSize(const FileName: string): Longint;
var
  SearchRec: TSearchRec;
begin
  if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then
    Result := SearchRec.Size
  else Result := -1;
  FindClose(SearchRec);
end;

CU
// This should work:
// The GetFileSize function retrieves the size, in bytes, of the specified file.
procedure TForm1.Button1Click(Sender: TObject);
var
  MySize : DWord;
  FileHandle : THandle;
  Arb : TOFSTRUCT;
begin
  MySize := 0;
  FileHandle := 0;
  FileHandle := OpenFile('c:/test',Arb,OF_EXIST);
  if FileHandle <> HFILE_ERROR then
  begin
    MySize := GetFileSize(FileHandle,Nil);
  end;
  showmessage(inttostr(MySize));
end;
Hmm, sorry that code should work but does not???
Rob ;-)
Avatar of pede

ASKER

Hi!

Jeurk's suggestion works, but I dont think any of the other ones do. Maybe Mike's stream, havent tried that.

I would like to give the points to Jeurk, if nobody disagrees too much.

/Pede
Humm, I agree with pede. I may be correct :)
A simple cut&paste and it's ok.
I have to say that the function is copied from the RXLib
fileutil.pas
Anyway it's working. PEde, if you want the filesize, maybe you
also need the position in the textfile ? I had that problem too.
SAdly the standard file function don't work with textfile. I
think it's because of the fact that the files are buffered by
windows or somewhat.
Regards.
Avatar of pede

ASKER

Hi Mike, Im rejecting your answer (for once), sorry 'bout that.

Jeurk: Yes, I need to know the position, but as I read 1 line at a time, I can just sum up the length. Would like to know your solution, though.

Propose an answer, and get the points.

Thanks, (all of you, of course)
Pede
function ResetIOResult: Integer;
begin
  Result := IOResult;
end;

function GetFileSize(const FileName: string): LongInt;
var
  Handle                      : Integer;
  OldErrorMode                : Word;
begin
  ResetIOResult;
  OldErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS);
  try
    Handle := FileOpen(FileName, fmOpenRead);
    if Handle < 0 then
      Result := 0
    else
      Result := FileSeek(Handle, 0, 2);
    FileClose(Handle);
  finally
    SetErrorMode(OldErrorMode);
  end;
end;
Humm,
Yes I would be glad but pvangundy was faster ;)

About the length, I'm doing exactly like you : Sum up
the total size of what I read.

CU
Avatar of pede

ASKER

Sorry pvangundy, but we already solved this one. Jeurk, propose an answer (hurry! hehe)

/Pede
ASKER CERTIFIED SOLUTION
Avatar of jeurk
jeurk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial