• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

Filesize of textfiles

Hi,

How can I get the size of a textfile? Filesize() does not work on textfiles.

/Pede

0
pede
Asked:
pede
  • 4
  • 3
  • 3
  • +3
1 Solution
 
RBertoraCommented:
Have you tried

DWORD GetFileSize(

    HANDLE  hFile,      // handle of file to get size of
    LPDWORD  lpFileSizeHigh,       // address of high-order word for file size
   );

Rob ;-)
0
 
rwilson032697Commented:
You can do this:

var
  f : file of byte;

....
assignfile(f, 'text.txt');
size := filesize(f);
....

Cheers,

Raymond.
0
 
LischkeCommented:
Hi pede,

just use either:

OpenFile + GetFileSize

or create a file stream and query its Size property.

Ciao, Mike
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
LischkeCommented:
Wow, I thought, I were fast...
0
 
jeurkCommented:
function GetFileSize(const FileName: string): Longint;
var
  SearchRec: TSearchRec;
begin
  if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then
    Result := SearchRec.Size
  else Result := -1;
  FindClose(SearchRec);
end;

CU
0
 
RBertoraCommented:
// This should work:
// The GetFileSize function retrieves the size, in bytes, of the specified file.
procedure TForm1.Button1Click(Sender: TObject);
var
  MySize : DWord;
  FileHandle : THandle;
  Arb : TOFSTRUCT;
begin
  MySize := 0;
  FileHandle := 0;
  FileHandle := OpenFile('c:/test',Arb,OF_EXIST);
  if FileHandle <> HFILE_ERROR then
  begin
    MySize := GetFileSize(FileHandle,Nil);
  end;
  showmessage(inttostr(MySize));
end;
0
 
RBertoraCommented:
Hmm, sorry that code should work but does not???
Rob ;-)
0
 
pedeAuthor Commented:
Hi!

Jeurk's suggestion works, but I dont think any of the other ones do. Maybe Mike's stream, havent tried that.

I would like to give the points to Jeurk, if nobody disagrees too much.

/Pede
0
 
jeurkCommented:
Humm, I agree with pede. I may be correct :)
A simple cut&paste and it's ok.
I have to say that the function is copied from the RXLib
fileutil.pas
Anyway it's working. PEde, if you want the filesize, maybe you
also need the position in the textfile ? I had that problem too.
SAdly the standard file function don't work with textfile. I
think it's because of the fact that the files are buffered by
windows or somewhat.
Regards.
0
 
pedeAuthor Commented:
Hi Mike, Im rejecting your answer (for once), sorry 'bout that.

Jeurk: Yes, I need to know the position, but as I read 1 line at a time, I can just sum up the length. Would like to know your solution, though.

Propose an answer, and get the points.

Thanks, (all of you, of course)
Pede
0
 
pvangundyCommented:
function ResetIOResult: Integer;
begin
  Result := IOResult;
end;

function GetFileSize(const FileName: string): LongInt;
var
  Handle                      : Integer;
  OldErrorMode                : Word;
begin
  ResetIOResult;
  OldErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS);
  try
    Handle := FileOpen(FileName, fmOpenRead);
    if Handle < 0 then
      Result := 0
    else
      Result := FileSeek(Handle, 0, 2);
    FileClose(Handle);
  finally
    SetErrorMode(OldErrorMode);
  end;
end;
0
 
jeurkCommented:
Humm,
Yes I would be glad but pvangundy was faster ;)

About the length, I'm doing exactly like you : Sum up
the total size of what I read.

CU
0
 
pedeAuthor Commented:
Sorry pvangundy, but we already solved this one. Jeurk, propose an answer (hurry! hehe)

/Pede
0
 
jeurkCommented:
An answer !
I happy it helped you.
CU
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now