PDA

Ver la Versión Completa : Conocer el espacio que ocupa un directorio


dec
30-06-2006, 23:05:49
Si necesitamos calcular el tamaño que nos ocupa un directorio, podemos hacer lo siguiente:


void __fastcall TForm1::Button1Click(TObject *Sender)
{
TSearchRec sr;
double tamanyo=0;
int iAttributes = 0;

if (FindFirst("c:\\windows\\*.*", iAttributes, sr) == 0)
{
tamanyo += sr.Size;
while (FindNext(sr) == 0)
tamanyo += sr.Size;
}
FindClose(sr);
ShowMessage(tamanyo);
}


Si este directorio contiene otros subdirectorios, y también queremos conocer el espacio que nos ocupan en disco...


#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
double size=0;

//---------------------------------------------------------------------------
double tamanyo(AnsiString Path)
{
TSearchRec R;
int F = FindFirst(Path + "\\*", faAnyFile, R);
while(F==0)
{
if(R.Name != "." && R.Name != "..")
{
if(R.Attr == faDirectory)
tamanyo(Path + "\\" + R.Name);
else
size+= R.Size;
}
F = FindNext(R);
}
FindClose(R);
return size;
}

__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner)
{
Label1->Caption = AnsiString(tamanyo("c:\\mirc"));
}
//---------------------------------------------------------------------------