> Jeg har et program som afspiller forskellige wav filer, afhængig af
> hvad man har valgt i en combobox. Når programmet starter, skal det
> søge et bibliotek igennem for wav filer, og tilføje dem til
> comboboxen, men hvordan gør jeg det ?
Jeg har fundet en klasse fra en af mit egne programmer, som kan scanne
biblioteker igennem for .WAV filer.
Koden kan sandsynligvis ikke compile sådan som den er her, men den er dog
testet, så den skulle fungere.
Den metode, som du i første omgang sandsynligvis vil finde mest interssant,
er "scandrive" (som iøvrigt også er rekursiv).
Den kan skanne et bibliotek og dets under-biblioteker igennem for .WAV
filer - de filnavne, den finder, lagres i en TList. Det kræver nok lige lidt
mere end det basale delphi viden at få dem ud derfra igen, men bare sig til
hvis du får brug for hjælp.
<kode>
unit filemanager;
interface
uses windows, classes, sysutils;
type
TScanDirEvent = procedure ( directory : string ) of object;
TFileEntry = record
filnavn : pchar;
sti : pchar;
attribs : dword;
fsize : int64;
end;
lptfileentry = ^TFileEntry;
TFileManager = class
private
files : tlist;
scandir : TScanDirEvent;
scanfile : TNotifyEvent;
totalsize : int64;
function GetSizeStr : string;
public
constructor Create;
destructor Destroy; override;
procedure ClearData;
function scanalldrives : boolean;
procedure scandrive ( dir : string );
procedure CreateFileEntry ( dir : string; filename : string );
function Count : integer;
function GetCounterStr : string;
procedure sort;
function GetPath ( index : integer ) : string;
property OnScanDir : TScanDirEvent read scandir write scandir;
property OnScanFile : TNotifyEvent read scanfile write scanfile;
function comparefunc (Item1, Item2: Pointer) : Integer;
function Len ( input : pchar ) : integer;
end;
implementation
uses dialogs;
constructor TFileManager.Create;
begin;
files := TList.Create;
scandir := nil;
scanfile := nil;
totalsize := 0;
inherited;
end;
destructor TFileManager.Destroy;
begin;
cleardata;
files.Destroy;
inherited;
end;
procedure TFileManager.ClearData;
var
i : integer;
fe : ^tfileentry;
begin;
i := 0;
while ( i < files.Count ) do begin;
fe := files.Items [i];
freemem (fe^.filnavn);
freemem (fe^.sti);
freemem (fe);
inc (i);
end;
files.Clear;
end;
function TFileManager.scanalldrives : boolean;
var
p : array [0..255] of char;
i, slen : integer;
dt : uint;
begin;
slen := GetLogicalDriveStrings (256, @p );
if (slen=0) then begin; refresh_counters; result := false; exit; end;
i := 0;
while ( i < slen ) do begin;
dt := GetDriveType ( @p [i] );
if ( dt = DRIVE_FIXED ) then begin;
self.scandrive ( p [i]+':\' );
end;
while not ( p [i] = #0 ) do inc (i);
inc (i);
end;
result := true;
end;
function TFileManager.Count : integer;
var
res : integer;
begin;
res := files.Count;
result := res;
end;
procedure TFileManager.CreateFileEntry ( dir : string; filename : string );
var
fe : lptfileentry;
fs : tfilestream;
begin;
new (fe);
fillchar ( fe^, 0, sizeof (tfileentry) );
getmem ( fe^.filnavn, length (filename)+1 );
getmem ( fe^.sti, length (dir)+1 );
strpcopy ( fe^.filnavn, filename );
strpcopy ( fe^.sti, dir );
fe^.attribs := 0;
try
fs := tfilestream.Create( dir+filename, fmOpenRead );
fe^.fsize := fs.Size;
fs.Destroy;
except
fe^.fsize := 0;
windows.Beep( 1000, 1 );
end;
files.Add( fe );
end;
procedure TFileManager.scandrive ( dir : string );
var
sr : TSearchRec;
res : integer;
begin;
if not (dir [length (dir)] = '\') then dir := dir + '\';
if assigned (scandir) then scandir ( dir );
res := FindFirst ( dir+'*.*', faAnyFile, sr );
if not (res)=0 then exit;
repeat;
// er det en directory?
if (sr.Attr and faDirectory) = faDirectory then begin;
if ( (length (sr.name)>0) and (not (sr.Name [1] = '.'))) then
self.scandrive( dir+sr.Name );
end else if uppercase (copy (sr.Name, length (sr.Name)-2, 3))='WAV' then
begin;
if assigned (scanfile) then scanfile ( self );
CreateFileEntry ( dir, sr.Name );
end;
until ( not (FindNext (sr) = 0) );
FindClose (sr);
end;
function TFileManager.Len ( input : pchar ) : integer;
var
t : integer;
begin;
t := 0;
while not ( input [t] = #0 ) do inc (t);
inc (t);
result := t;
end;
function TFileManager.comparefunc (Item1, Item2: Pointer) : Integer;
begin;
result := CompareStr ( ReturnArtistTitle( Item1 ), ReturnArtistTitle(
Item2 ) );
end;
procedure TFileManager.sort;
begin;
files.Sort( comparefunc );
end;
function TFileManager.GetPath ( index : integer ) : string;
var
p : lptfileentry;
s,f : string;
begin;
p := files.Items [index];
s := p^.sti;
f := p^.filnavn;
result := s+f;
end;
function TFileManager.GetSizeStr : string;
var
h, i : string;
c, c2 : integer;
begin;
h := inttostr (totalsize);
i := '';
c2 := length (h);
c := 0;
while (c2>0) do begin;
if c<3 then begin;
i := i + h [c2];
inc (c);
dec (c2);
end else begin;
c := 0;
i := i + '.';
end;
end;
h := i;
i := '';
c2 := length (h);
while (c2>0) do begin;
i := i + h [c2];
dec (c2);
end;
result := i;
end;
begin;
end.
<//kode>
|