Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

星期二, 10月 16, 2007

開啟資料夾並搜尋檔案及得知其檔案大小


此可以做為新增MP3歌曲的功能,只要加入某個最上層目錄,就可以自動加入所有子目錄及其音樂。

新增2個TButton、1個TEdit、1個TMemo(ScrollBars屬性ssBoth)
兩個按鈕皆點兩下加入事件

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, FileCtrl;

type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
procedure GetFileSize(sPath : string);
{ Private declarations }
public
{ Public declarations }
end;

var
Form1 : TForm1;
Path : string;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
Dir: String;
begin
Dir := '';
if SelectDirectory('選擇文件夾', '', Dir) then
begin
if Copy(Dir,Length(Dir),1) <> '\' then
Dir := Dir + '\';
Edit1.text := Dir; //Edit1顯示路徑
Path := Dir; //Path全域變數記錄Dir
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
GetFileSize(Edit1.text);
end;

procedure TForm1.GetFileSize(sPath : string);
var
hFind : THandle;
filename, fPath : string;
nSize : Int64;
fd : WIN32_FIND_DATA;
begin
hFind:=Windows.FindFirstFile(PChar(sPath + '*.*'), fd); //所有檔案都找
if(hFind <> INVALID_HANDLE_VALUE) then
begin
repeat
filename:=fd.cFileName;
fPath := sPath + filename;
if((filename = '.') or (filename = '..')) then //不是上層 或 上上層
Continue;
if(fd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) > 0 then //是否為目錄
begin
GetFileSize(fPath + '\'); //遞迴呼叫 跑子目錄
end else begin
nSize:=fd.nFileSizeHigh;
nSize:=nSize shl 32;
nSize:=nSize or fd.nFileSizeLow;
Memo1.lines.add(fPath+' Size='+inttostr(nSize)=' bytes');
end;
until (not Windows.FindNextFile(hFind, fd));
Windows.FindClose(hFind);
end;
end;

end.

沒有留言: