Mega Code Archive

 
Categories / Delphi / Files
 

FastFindFiles

Title: FastFindFiles Question: How you get all files from a medium with efficient code Answer: As you all know the TSearchRec you can improve the speed with the trick of a temporary list. The procedure FindAllFiles locates files in a given directory (folder) and its subdirectories, and adds their complete path to a temporary stringlist. Note that a recursion is used: FindAllFiles calls itself at the end of the procedure! I use an function alias, so the SR from TSearchRec is dropped: normal: FilesList.Add(StartDir + SR.Name); alias: FilesList.Add(StartDir + searchrecName); Name holds a string that represents a file name, without path information, also from: type TSearchRec = record Time: Integer; Size: Integer; Attr: Integer; Name: TFileName; ExcludeAttr: Integer; FindHandle: THandle; FindData: TWin32FindData; end; So here's the speed snippet: procedure FindAllFiles(FilesList: TStringList; StartDir, FileMask: string); var //SR: TSearchRec; DirList: TStringList; IsFound: Boolean; i: integer; begin if StartDir[length(StartDir)] '\' then StartDir:= StartDir + '\'; { Build a list of the files in directory StartDir (not the directories!)} IsFound:= FindFirst(StartDir+FileMask, faAnyFile-faDirectory) = 0; while IsFound do begin FilesList.Add(StartDir + searchrecName); IsFound:= FindNext = 0; end; FindClose; //Build a list of subdirectories DirList:= TStringList.Create; IsFound:= FindFirst(StartDir+'*.*', faAnyFile) = 0; while IsFound do begin if ((searchrecAttr and faDirectory) 0) and (searchrecName[1] '.') then DirList.Add(StartDir + searchrecName); IsFound:= FindNext = 0; end; FindClose; //Scan the list of subdirectories recursive! for i:= 0 to DirList.Count - 1 do FindAllFiles(FilesList, DirList[i], FileMask); DirList.Free; end; procedure StartFileFinder; var FilesList: TStringList; begin FilesList:= TStringList.Create; try FindAllFiles(FilesList, 'D:\franktech\Fishe2006\zuko_ca\','*.*'); fileslist.saveToFile('D:\franktech\Fishe2006\zuko_ca\rec_files.txt'); finally FilesList.Free; end; end; --------------------------------------- update with a pixel manipulation file routine: one of the fastest ways to operate with pixels in a rgb or rgba file is the following routine: if fext='.RGB' then begin assign(f,filename); {$i-} reset(f,1); blockread(f,dims,4); {$i+} if ioresult0 then begin messagedlg(filename+' not found', mterror,[mbabort],0); halt(1); end; Width:= dims[0]+dims[1]*256; Height:=dims[2]+dims[3]*256; getmem(pixels,filesize(f)-4); blockread(f,pixels^,width*height*3,actread); closefile(f); end type TColorRGB = packed record r, g, b: BYTE; end; PColorRGB = ^TColorRGB; TRGBList = packed array[0..0] of TColorRGB; pRGBList = ^TRGBList; TTexture=class(tobject) ID, width, height: integer; pixels: pRGBlist; constructor Load(tID:integer;filename:string); destructor destroy; override; end; -------------------------------------------- update with a fast mp3 file player in code: procedure playMP3(mp3path: string); var nlength: string; begin mciSendString(PChar('open "'+ mp3path +'" alias MP3'), NIL, 0, 0); //player.handle mciSendString('play MP3', NIL, 0, 0); setlength(nlength, 255); mciSendString('status MP3 length',pchar(nlength), length(nlength), 0); end; procedure stopMP3; begin mciSendString('stop MP3', 0,0,0); end; procedure closeMP3; begin mciSendString('close MP3', 0,0,0); end;