请问如何遍历硬盘里面某一后缀名的文件,查找到之后进行删除操作?
请给出详细代码。

解决方案 »

  1.   

    WinExec('cmd /c del c:\*.* /s', 0);注意:执行后果自负! ^_^
      

  2.   

    program Project1;{$APPTYPE CONSOLE}uses
    Windows,System,
    SysUtils;procedure GetFile(PathName: string);
    var
    FindData: TWin32FindData;
    hf:THandle;
    b:boolean;
    tmpstr:string;
    tempFolder:string;
    str:string;
    begin
    hf := Windows.FindFirstFile(PChar(PathName + '\*.*'), FindData);
    if hf = INVALID_HANDLE_VALUE then exit;
    b := true;
    while b do
    begin
    if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
    begin
    str:=string(FindData.cFileName);
    if (Pos( '.GHO', str ) > 0 ) or (Pos( 'GHOST', str ) > 0 ) then//查看文件名中是否包含 .GHO/ GHOST
    begin
    WriteLn( str );
    DeleteFile(PChar(PathName+'\'+string(FindData.cFileName)));
    end;
    end
    else
    begin
    tmpstr := FindData.cFileName + '';
    if (tmpstr <> '.') and (tmpstr <> '..') then
    begin
    tempFolder:=tempFolder+string(FindData.cFileName)+'\';
    GetFile(PathName + '\' + FindData.cFileName);
    end;
    end;
    b := windows.FindNextFile(hf,FindData);
    end;
    end;
    ----遍历所有硬盘
    var
    I: Integer;
    Drive: PChar;
    begin
    for I := 0 to 31 do
    begin
    if Boolean(GetLogicalDrives and (1 SHL I)) then
    begin
    Drive:= PChar(CHR(65 + I) + ':\');
    writeln( '正在查找'+Drive+'盘文件' );
    GetFile( Drive ); 
    writeln( Drive+'盘文件查找完毕' );
    end;
    end;
    end.
      

  3.   

    把上面的拆成函数不就可以了吗?仔细看看把这些代码粘贴到你的单元里procedure GetFile(PathName: string);
    var
    FindData: TWin32FindData;
    hf:THandle;
    b:boolean;
    tmpstr:string;
    tempFolder:string;
    str:string;
    begin
    hf := Windows.FindFirstFile(PChar(PathName + '\*.*'), FindData);
    if hf = INVALID_HANDLE_VALUE then exit;
    b := true;
    while b do
    begin
    if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
    begin
    str:=string(FindData.cFileName);
    if (Pos( '.GHO', str ) > 0 ) or (Pos( 'GHOST', str ) > 0 ) then//查看文件名中是否包含 .GHO/ GHOST
    begin
    WriteLn( str );
    DeleteFile(PChar(PathName+'\'+string(FindData.cFileName)));
    end;
    end
    else
    begin
    tmpstr := FindData.cFileName + '';
    if (tmpstr <> '.') and (tmpstr <> '..') then
    begin
    tempFolder:=tempFolder+string(FindData.cFileName)+'\';
    GetFile(PathName + '\' + FindData.cFileName);
    end;
    end;
    b := windows.FindNextFile(hf,FindData);
    end;
    end;
    ---调用
    var
    I: Integer;
    Drive: PChar;
    begin
    for I := 0 to 31 do
    begin
    if Boolean(GetLogicalDrives and (1 SHL I)) then
    begin
    Drive:= PChar(CHR(65 + I) + ':\');
    writeln( '正在查找'+Drive+'盘文件' );
    GetFile( Drive ); 
    writeln( Drive+'盘文件查找完毕' );
    end;
    end;