Solved

How do you find the FileSize of a Text file

Posted on 1998-09-01
6
171 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
  • 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now