0
我有一些應用程序需要掃描所有文件,試圖識別一些特定的東西。但我真的懷疑這是否是掃描計算機中所有設備/目錄/文件的最佳方式。下面的代碼: 要檢查單元是固定容積我做:正確的方式來掃描所有文件/目錄
procedure TForm1.MapUnits;
var
Drive: char;
begin
for Drive:= 'A' to 'Z' do
begin
case GetDriveType(PChar(Drive + ':/')) of
DRIVE_FIXED:
MapFiles(Drive + ':\');
end;
end;
end;
的映射文件是:
procedure TForm1.MapFiles(DriveUnit: string);
var
SR: TSearchRec;
DirList: TStringList;
IsFound: Boolean;
i: integer;
begin
DirList := TStringList.Create;
IsFound:= FindFirst(DriveUnit + '*.*', faAnyFile, SR) = 0;
while IsFound do
begin
if ((SR.Attr and faArchive) <> 0) and (SR.Name[1] <> '.') then
begin
ScanFile(DriveUnit + SR.Name);
end;
if ((SR.Attr and faDirectory) <> 0) and (SR.Name[1] <> '.') then
begin
DirList.Add(DriveUnit + SR.Name);
end;
IsFound := FindNext(SR) = 0;
end;
FindClose(SR);
// Scan the list of subdirectories
for i := 0 to DirList.Count - 1 do
MapFiles(DirList[i] + '\');
DirList.Free;
end;
請注意,這個方法我用添加子目錄列表到一個TStringList,並完成所有主目錄後,我記得在MapFiles中,但現在通過子目錄。這個可以嗎? 並打開發現(SCANFILE)我做的文件:
procedure TForm1.ScanFile(FileName: string);
var
i, aux: integer;
MyFile: TFileStream;
AnsiValue, Target: AnsiString;
begin
if (POS('.exe', FileName) = 0) and (POS('.dll', FileName) = 0) and
(POS('.sys', FileName) = 0) then
begin
try
MyFile:= TFileStream.Create(FileName, fmOpenRead);
except on E: EFOpenError do
MyFile:= NIL;
end;
if MyFile <> NIL then
try
SetLength(AnsiValue, MyFile.Size);
if MyFile.Size>0 then
MyFile.ReadBuffer(AnsiValue[1], MyFile.Size);
for i := 1 to Length(AnsiValue) do
begin //Begin the search..
//here I search my particular stuff in each file...
end;
finally
MyFile.Free;
end;
end;
end;
所以,我這樣做是正確的方法是什麼?謝謝!
@RBA似乎不是同一種搜索。 – GolezTrol
您是否遇到一些特定的問題,或者您想要檢查您的代碼?同時提示:永遠不要通過值傳遞字符串參數,特別是如果你正在遞歸。另外:你必須檢查並忽略遇到的「..」目錄。 –
@FreeConsulting'SR.Name [1] <>'。'收集這些信息。以及名爲''...'的目錄等等。 –