Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do you find the FileSize of a Text file

Posted on 1998-09-01
6
Medium Priority
?
180 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 300 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
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!

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

688 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