Solved

Directory & File Size

Posted on 2002-04-30
4
133 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

12 Experts available now in Live!

Get 1:1 Help Now