Solved

File Size

Posted on 1998-12-31
11
365 Views
Last Modified: 2010-04-04
How can I find the file size of an image file and display the size in say a Label Caption?
I am using a FileListBox to click on to Open the File.
Using Delphi4.
Have tried the function in the help section but hangs up when trying to open a file previously opened.
0
Comment
Question by:xpher
  • 6
  • 5
11 Comments
 
LVL 1

Author Comment

by:xpher
ID: 1353910
Edited text of question
0
 
LVL 20

Accepted Solution

by:
Madshi earned 50 total points
ID: 1353911
Hi xpher,

function fio_GetFileSize(const FileName: string) : int64;
var Handle   : THandle;
    FindData : TWin32FindData;
begin
  Handle:=FindFirstFile(PChar(FileName),FindData);
  if Handle<>INVALID_HANDLE_VALUE then begin
    Windows.FindClose(Handle);
    if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY)=0 then begin
      result:=(int64(FindData.nFileSizeHigh) shl 32)+int64(FindData.nFileSizeLow);
      exit;
    end;
  end;
  result:=-1;
end;

Regards, Madshi.

Hmm. On the other side: Why is the file still open? You should check this, because it's not very clean to leave a file open for a long time...
0
 
LVL 1

Author Comment

by:xpher
ID: 1353912
Thanks Madshi it works great. Only problem now is to try and understand it.
I think my original problem was that I was naive enough to think that clicking on another file would automatically close down the other. I'll have to investigate how to close the image before opening another.

Thanks again
Chris:))

ps Happy New Year
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 20

Expert Comment

by:Madshi
ID: 1353913
Happy new year...  :-)

Hmm. How do you open the image files? Perhaps I can then tell you how to close them again...

Regards, Madshi.
0
 
LVL 1

Author Comment

by:xpher
ID: 1353914
Hi Madshi
Thanks for comment.

I open the image files by clicking on the file name in the file list box.

procedure TForm1.FileListBox1Click(Sender: TObject);
begin
Image1.Picture.LoadFromFile(FileListBox1.Filename);
end;

This all works fine until I try the function for file size shown in Delphi help as mentioned earlier.

Many thanks for helping. Look forward to a reply.

Regards
Chris :))
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1353915
Hi Chris,

hmmm. I don't understand it. Here's the source from Delphi4, that is called when you call Image1.Picture.LoadFromFile. It should be the same code with Delphi2/3:

procedure TGraphic.LoadFromFile(const Filename: string);
var
  Stream: TStream;
begin
  Stream := TFileStream.Create(Filename, fmOpenRead or fmShareDenyWrite);
  try
    LoadFromStream(Stream);
  finally
    Stream.Free;
  end;
end;

So with "Stream.Free" the file is properly closed again. So I really don't understand why you've had problems with the other filesize function.
Could you please (1) tell me the name of the other function and (2) try to call it before loading the image? Does it work, then?

Regards, Madshi.
0
 
LVL 1

Author Comment

by:xpher
ID: 1353916
Hi Madshi
Thanks for comment.

I open the image files by clicking on the file name in the file list box.

procedure TForm1.FileListBox1Click(Sender: TObject);
begin
Image1.Picture.LoadFromFile(FileListBox1.Filename);
end;

This all works fine until I try the function for file size shown in Delphi help as mentioned earlier.

Many thanks for helping. Look forward to a reply.

Regards
Chris :))
0
 
LVL 1

Author Comment

by:xpher
ID: 1353917
Hi Madshi
Thanks for comment.

I open the image files by clicking on the file name in the file list box.

procedure TForm1.FileListBox1Click(Sender: TObject);
begin
Image1.Picture.LoadFromFile(FileListBox1.Filename);
end;

This all works fine until I try the function for file size shown in Delphi help as mentioned earlier.

Many thanks for helping. Look forward to a reply.

Regards
Chris :))
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1353918
Hmmm. Are you stuttering?    :-)
0
 
LVL 1

Author Comment

by:xpher
ID: 1353919
oops! don't know what happened seems to have posted comments again sorry. ignore last two.

In response to your last comment, the function name I was trying to use was FileSize. In the help section it gives FileSize, Seek, FilePos Example. I had tried butchering this to just use file size. I've now tried again and realised that I forgot the last line to close the file.

This is what I have ended up with (from D4 help) and it works now placing FileSize into a Label Caption:

procedure TForm1.FileListClick(Sender: TObject);
  var
   f: file of Byte;
   size : Longint;
   S: string;

 begin
     AssignFile(f, FileList.FileName);
     Reset(f);
     size := FileSize(f);
     S := 'File size in bytes: ' + IntToStr(size);
     Seek(f,size div 2);
     CloseFile(f);
 begin
   Image1.Picture.LoadFromFile(FIleList.Filename);
   Label1.Caption := s;
   end;
 end;

 Many thanks for all your help and getting me to think. Hope to communicate again.

Cheers
Chris :-))
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1353920
:-)

See you...

Regards, Madshi.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Path  to current project in Delphi. 2 79
delphi prevent click fast 2 212
code issue 8 127
How to load 2 images in same column in Delphi 2 45
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 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

19 Experts available now in Live!

Get 1:1 Help Now