Filesize of textfiles

Hi,

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

/Pede

LVL 1
pedeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RBertoraCommented:
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 ;-)
0
rwilson032697Commented:
You can do this:

var
  f : file of byte;

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

Cheers,

Raymond.
0
LischkeCommented:
Hi pede,

just use either:

OpenFile + GetFileSize

or create a file stream and query its Size property.

Ciao, Mike
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

LischkeCommented:
Wow, I thought, I were fast...
0
jeurkCommented:
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
0
RBertoraCommented:
// 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;
0
RBertoraCommented:
Hmm, sorry that code should work but does not???
Rob ;-)
0
pedeAuthor Commented:
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
0
jeurkCommented:
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.
0
pedeAuthor Commented:
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
0
pvangundyCommented:
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;
0
jeurkCommented:
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
0
pedeAuthor Commented:
Sorry pvangundy, but we already solved this one. Jeurk, propose an answer (hurry! hehe)

/Pede
0
jeurkCommented:
An answer !
I happy it helped you.
CU
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.