Solved

Directory & File Size

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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