Solved

How do you find the FileSize of a Text file

Posted on 1998-09-01
6
177 Views
Last Modified: 2010-04-04
How do you find the FileSize of a TextFile when the FileSize function doesn't work on Text files?
0
Comment
Question by:jwalker14
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
Lischke earned 100 total points
ID: 1338304
You can use the API function GetFileSize. You have to open the file first to get a file handle (use CloseHandle to close it after use). OpenFIle has some limitations (path length etc.). Use CreateFile with OPEN_EXISTING flag instead to avoid them.

Ciao, Mike
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1338305
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
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1338306
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jwalker14
ID: 1338307
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!
0
 
LVL 10

Expert Comment

by:Lischke
ID: 1338308
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
0
 

Author Comment

by:jwalker14
ID: 1338309
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question