Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Directory & File Size

Posted on 2002-04-30
4
Medium Priority
?
153 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 360 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

609 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