Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding largest file 2

Status
Not open for further replies.

purcion

Programmer
Feb 14, 2005
32
AU
I basically need some help with code (delphi) that will display the files on my system sorted by size. This could be a problem i guess if a system has 90,000 files but i would even be happy to have code that displays just the largest file on my system. thanks in advance
 
Can you post the code that you have developed so far and tell us what the actual problem is?

Andrew
Hampshire, UK
 
Ok thanks here is some code

//================================================
procedure ListFileDir(Path: string; FileList: TStrings);
var
SR: TSearchRec;
begin
if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then
begin
repeat
if (SR.Attr <> faDirectory) then
begin
FileList.Add(SR.Name);
end;
until FindNext(SR) <> 0;
FindClose(SR);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ListFileDir('C:\Windows\', ListBox1.Items);
end;
//======================================================
The problem I have is I dont know how to get a " system wide " list (every file on the system)
and how to get the file size as I really just want to find out what files on my system are the largest .. I have a alot of media files ( 3d renderings ect. )I would like to create a quick utility for finding the main hdd space offenders and make my file culling choices based on it.
Thanks for your time
 
The best approach to looking at all the directory entries on the disc is to use a recursive search funciton.

The code you have supplied is correct except that where the file attribute indicates a directory you need to search within that directory. The best way to do that is to call your ListFileDir procedure again (this is the recursive bit).

One thing to watch out for is to ignore the directories with names . and .. as these will lead you into a never ending loop.

The file size is simply SR.Size.

Finding just the largest file on your hard disk is easier than listing the 100 largest files. However, if it turns out that your largest file is the system paging file then you are no better off as you must not delete that.

So you will need to keep track of your 100? largest files. There are a number of ways of doing this but I would keep a sorted StringList where the first 10 characters of each string is the file size and the remaining part of the string is the full path name of the file.

Have a go at writing the code to do this. You will learn so much more than by one of us doing it for you.


Andrew
Hampshire, UK
 
This snippet return the largest file, the rest is up to you :)

Code:
procedure GetLargestFile(const PathName, FileMask: string; const SubDirs: Boolean; var LargestFile: String; var LargestSize: Integer);
var
  Rec: TSearchRec;
begin
  if FindFirst(PathName + FileMask, faAnyFile - faDirectory, Rec) = 0 then
  try
    repeat
      if Rec.Size >= LargestSize then
      begin
        LargestFile := PathName+Rec.Name;
        LargestSize := Rec.size;
      end;
    until FindNext(Rec) <> 0;
  finally
    FindClose(Rec);
  end;

  if not SubDirs then Exit;

  if FindFirst(PathName + '*.*', faDirectory, Rec) = 0 then
  try
    repeat
      if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name<>'.') and (Rec.Name<>'..') then
        GetLargestFile(PathName + Rec.Name, FileMask, True, LargestFile, LargestSize);
    until FindNext(Rec) <> 0;
  finally
    FindClose(Rec);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  s: String;
begin
  GetLargestFile('C:\ABS\', '*.*', true, s, i);

  Memo1.Lines.Add(s);
  memo1.lines.add(inttostr(i));
end;

KungTure-RX.jpg

//Nordlund
 
Thanks that is some really good help , much appreciated to both postings, now I will attempt to make it recursive . A few touch ups here and there hey presto and away it goes thanks again ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top