Solved

How do you find the FileSize of a Text file

Posted on 1998-09-01
6
172 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

912 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

24 Experts available now in Live!

Get 1:1 Help Now