Solved

Directory & File Size

Posted on 2002-04-30
4
136 Views
Last Modified: 2010-04-04
Hi Experts

Two questions today
How would I be able to extract file name from a given path.
And anyone knows of code or a component that would calculate the size of a file or drive.

Thank you
0
Comment
Question by:Hafez
4 Comments
 
LVL 5

Expert Comment

by:alanwhincup
ID: 6981838
Here you are:

1: ExtractFileName('C:\blahblah.txt);

2: Could be done like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  SearchRec : TSearchRec;
begin
  FindFirst('C:\blahblah.blah', faAnyFile, SearchRec);
  if SearchRec.Size <> 0 then
    ShowMessage(Inttostr(SearchRec.Size));
end;

Or with the use of FileSizeByName in IdGlobal unit or you may be able to open the file in read-only mode with CreateFile() then get the size using GetFileSize().

3: Look at http://www.efg2.com/Lab/Library/Delphi/IO/D3DiskSpaceKludge.ZIP for disk size

Cheers,

Alan
0
 
LVL 33

Expert Comment

by:Slick812
ID: 6982579
here's a function I use for drive size


type
TdriveSize = record
    FreeS:Int64;
    TotalS:Int64;
end;


function DiskSpace(Drive: String): TdriveSize;
var
  TSpace,TotalS: Int64;
  Sizes:TdriveSize;
  ErrorMode: Word;
begin
Delete(Drive, 4, 600);
ErrorMode := SetErrorMode(SEM_FailCriticalErrors);
if GetDiskFreeSpaceEx(@Drive[1], TSpace, TotalS, nil) then
begin
Sizes.FreeS :=  TSpace;
Sizes.TotalS := TotalS;
Result := Sizes;
end else
begin
Sizes.FreeS := -1;
Sizes.TotalS := -1;
Result := Sizes;
end;
SetErrorMode(ErrorMode);
end;
0
 
LVL 6

Accepted Solution

by:
Stuart_Johnson earned 90 total points
ID: 6982677
Here's a bit of code I use to get the total size of a given directory and all it's sub directories.  I know you didn't ask for this, but I thought it maybe useful to know :)

function GetDirectorySize(Dir: String): Double;
var
  SR: TSearchRec;
  I: Integer;
begin
  Dir := IncludeTrailingBackSlash(Dir);

  I := FindFirst(Dir + '*.*', faAnyFile, SR);
  while I = 0 do
    begin
      if (SR.Name[1] <> '.') then //exclude '.' and '..' directories
        begin
          if (SR.Attr and faDirectory) = faDirectory then
            Result := Result + GetDirectorySize(Dir + SR.Name)  //calculate size of nested directory
          else
            Result := Result + SR.Size;
        end; {if Name <> '.'}
      I := FindNext(SR);
    end; {while I = 0}

  FindClose(SR);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(Format('Total Size of Directory is: %n bytes', [GetDirectorySize('D:\Programming')]));
end;
0
 

Author Comment

by:Hafez
ID: 6982811
Its exactlty what I was looking for , Thank you
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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

14 Experts available now in Live!

Get 1:1 Help Now