Solved

Directory & File Size

Posted on 2002-04-30
4
148 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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