Link to home
Start Free TrialLog in
Avatar of jwalker14
jwalker14

asked on

How do you find the FileSize of a Text file

How do you find the FileSize of a TextFile when the FileSize function doesn't work on Text files?
ASKER CERTIFIED SOLUTION
Avatar of Lischke
Lischke

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
Let's say you have a memo that has the text from the file.(this is because we don;t want to get in details right now)....

To see how much the a text file takes place try this.....

procedure TForm1.Button2Click(Sender: TObject);
var
  S : Real;
begin
  S := Length(Memo1.text) / 1024;//Get the number in kb not bytes
  ShowMessage(Format('%.1f''kb''', [s]));//Show the floating point value as example 2.1kb
end;

Hope this helps....
Regards,
Viktor Ivanov
Example without using Memo comp.....
---------------
procedure TForm1.Button2Click(Sender: TObject);
var
  S : Real;
  f : TextFile;
  k,z : String;
begin
  AssignFile(F, 'C:\windows\desktop\FindFuncInWebBrowser.txt');
  try
    Reset(F);
    Z := '';
    while not EOF(F) do
    begin
      ReadLn(F, k);
      Z := Z + K;
    end;
    S :=Length(Z) / 1024;
    ShowMessage(Format('%.1f''kb''', [s]));
  finally
    CloseFile(F);
  end;
end;
---------------
Regards,
Viktor Ivanov
Avatar of jwalker14
jwalker14

ASKER

This is a good solution but requires opening and closing the file.  A co-worker was helping me on this this morning and suggested the following solutiong;

var
    FileSize  : integer;
    DirRec    : TSearchRec;

begin
    FileName := 'C:\Windows\myfile.txt';
    FindFirst(FileName, faAnyFile, DirRec);
    FileSize   := DirRec.Size;
end;

this could be either a function or procedure...

Your answer was very good and I appreciate the quick response...Thanks for all your help!
Well, I was thinking about this solution too, but had to realize that FindFirst/FindNext on a NT machine over a network needs a remarkable amount of time, so I suggested the other one.

Thanks and happy programming

Ciao, Mike
I was not aware of the degredation in performance on NT networks.  Luckily were not using NT here...however, I appreciate your thoughts and thanks again for the help!

Jim Walker