Recent

Author Topic: Bytes in file  (Read 724 times)

guest48704

  • Guest
Bytes in file
« on: July 24, 2019, 11:33:38 pm »
  I have this code that returns the bytes in a file, except it will only give
the byte count for a file that is in the same folder as the executable.

Code: [Select]
procedure TForm1.FileBytes(FileNm : string);
var f : textfile; ttlBytes, AHandle : integer;
begin
     //get bytes in file
     AHandle := FileOpen(PChar(FileNm), fmShareDenyNone);
     ttlBytes := GetFileSize(AHandle, nil);
     FileClose(AHandle);

     ShowMessage(inttostr(ttlBytes));

  Executing like this: FileBytes('C:\folder\file.txt'); give a file size of -1.

Anyone know of a way to get it to work with the path included?

Thanks

guest48704

  • Guest
Re: Bytes in file
« Reply #1 on: July 24, 2019, 11:44:05 pm »
Got it to work this way:

Code: [Select]
procedure TForm1.Test1Click(Sender: TObject);
var
   infile: file of byte;
   Infilename: string;
   size: longint;
begin
infilename := 'C:\Lazarus\EncryptDecrypt\afolder\myfile';
Assignfile(infile, infilename);

size := FileSize(infilename);
ShowMessage(inttostr(size));
end;

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Bytes in file
« Reply #2 on: July 24, 2019, 11:49:40 pm »
Try it the pascal way:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileBytes (Fname : String);
  2. var f : file of byte; bytes: integer;
  3.  
  4. begin
  5. assignFile (f, fname);
  6. reset (f);
  7. bytes := Filesize (f);
  8. closeFile(f);
  9. showMessage (IntToStr(bytes));
  10. end;
  11.  

Winni

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Bytes in file
« Reply #3 on: July 24, 2019, 11:51:44 pm »
Only silver medal. Sighh!

Winni

 

TinyPortal © 2005-2018