如果遍历PE文件呀
我想:
遍历除系统盘外的所有EXE文件,然后在过滤大小在5M以上的,然后在过滤检查剩下的是不是PE文件(PE文件的前俩字节是MZ,其实不要着步也可以,尽量提供把),
然后把所有剩下的用MEMO显示出来
比如
d:\1.exe
d:\2.exe
让代码尽量简短.

解决方案 »

  1.   

    var
      dwDrivers: LongWord;
      I: Integer;
      sysDir: AnsiString;
      strDriver,strCurrentDir: AnsiString;  FindFileData: TWIN32FindData;
      hFindHandle: THandle;  dirList: TStringList;
    begin
      memo1.Lines.BeginUpdate;
      dirList := TStringList.Create;
      try
       dwDrivers := GetLogicalDrives;
       SetLength(sysDir,max_path);
       SetLength(sysDir,GetSystemDirectory(PAnsiChar(sysDir),MAX_PATH));
       sysDir := ExtractFileDrive(sysDir);
       for I := 0 to 32 - 1 do
        if 1= ((dwDrivers shr I) and 1) then begin      strDriver :=Chr(65 + i) + ':';
          if strDriver <> sysDir then begin
            //非系统盘
            dirList.Add(strDriver);
          end;
        end;    while dirList.Count > 0 do begin
          strCurrentDir := dirList.Strings[0];
          dirList.Delete(0);      FillChar(FindFileData,sizeof(TWIN32FindData),0);
          hFindHandle := FindFirstFile(PAnsiChar(strCurrentDir+'\*.*'),FindFileData);
          if hFindHandle = INVALID_HANDLE_VALUE then continue;
          try
            if (FindFileData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY then begin
              if (AnsiString(FindFileData.cFileName) <> '.') and (AnsiString(FindFileData.cFileName) <> '..') then begin
                dirList.Add(strCurrentDir + '\' + AnsiString(FindFileData.cFileName));
              end;
            end else begin
              if    (UpperCase(RightStr(FindFileData.cFileName,4)) = '.EXE')
                and ((FindFileData.nFileSizeLow > 5*1024*1024) or (FindFileData.nFileSizeHigh <> 0)) then begin
                //输出
                Memo1.Lines.Add(strCurrentDir + '\' + AnsiString(FindFileData.cFileName));
              end;
            end;
          finally
            Windows.FindClose(hFindHandle);
          end;
        end;  finally
        dirList.Free;
        memo1.Lines.EndUpdate;
      end;
    end;
      

  2.   

    我说呢怎么没作用呢,原来
     //非系统盘
            dirList.Add(strDriver);
    这个
    strDriver
    是G盘....
    我电脑上没有G盘所以没效果
    我说的是遍历除了系统盘以外所有的驱动器,你给的代码是遍历G盘....
    还是自己慢慢弄把
    其他人能给提示就给把
    十分感谢
      

  3.   


       //取得所有逻辑盘符掩码
       dwDrivers := GetLogicalDrives;
       
       SetLength(sysDir,max_path);
       //取得系统路径
       SetLength(sysDir,GetSystemDirectory(PAnsiChar(sysDir),MAX_PATH));
       //取得系统路径所在的盘符,即系统盘
       sysDir := ExtractFileDrive(sysDir);
       for I := 0 to 32 - 1 do
        if 1= ((dwDrivers shr I) and 1) then begin      strDriver :=Chr(65 + i) + ':';
          if strDriver <> sysDir then begin
            //非系统盘时添加进扫描列表
            dirList.Add(strDriver);
          end;
        end;
      

  4.   

    unsigned 的方法是人为的构造盘符C:\,接着再构造D:\,然后构造C:\,等等,这样可能导致并不存在的盘符也被构造出来降低效率。其实可以直接用API函数GetLogicalDriveStrings取得当前磁盘总数,只不过它们是以Nil分隔的。需要替换一下才能全部显示。
    procedure TForm1.Button1Click(Sender: TObject);
    var
      p:array[0..105] of char;
      j:integer;
      s:TStringList;  sysDir:array[0..100] of char;
    begin
     GetSystemDirectory(sysDir,99);
     sysDir[3]:=#0; //截到系统盘!j:=GetLogicalDriveStrings(104,p);//将整个字符串的长度保存到变量中。
    while j>0 do
     begin //将所有空字符转为空格。
       if p[j]=#0 then p[j]:=' ';
       j:=j-1;
     end;
    showmessage('取到机器所有盘符:'+p);
    s:=Tstringlist.Create;
    s.Delimiter:=' ';//以空格来拆分整个字符串。
    s.DelimitedText:=p;
    for j:=0 to s.Count-1 do
      if GetDriveType(PChar(s.Strings[j]))=DRIVE_FIXED then
        if lstrcmpi(sysDir,PChar(s.Strings[j]))<>0 then
          showmessage(s.Strings[j]);//如果是硬盘而且不是系统盘的话则显示。
    s.Free;
    end;
      

  5.   

    可不可以检测磁盘是否就绪呀,
    A盘是软盘没查软盘不可写入文件
    G盘是DVD启动器...
    可不可以检测可以写如文件的盘符
      

  6.   

    没看到7楼有答案了,
    谁能写出完整的
    遍历除系统盘外的所有EXE文件,然后在过滤大小在5M以上的,然后在过滤检查剩下的是不是PE文件(PE文件的前俩字节是MZ,其实不要着步也可以,尽量提供把), 
    然后把所有剩下的用MEMO显示出来 如果插入U盘,那U盘也要遍历
      

  7.   

    你就懒到这种程度?还写什么程序,干脆发到http://prj.csdn.net.
      

  8.   


    看来你是个新手,我给写一个吧。复制U盘的只需要判断磁盘类型就可以了。你自己做吧。你在贴子提问的时候未说明,现在才说,提新问题请开新贴。启动DELPHI建个新工程,然后在窗体上放两个按扭,再放一个Memo控件。复制下面的代码即可:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}uses StrUtils;procedure DeletePriorRecord(sPath:String);
    var
      sr:TSearchRec;
    begin
      if sPath[length(sPath)]<>'\' then sPath:=sPath+'\';
      if FindFirst(sPath+'*.*',faAnyFile ,sr)=0 then
        begin
          repeat
            Application.ProcessMessages;
            if(sr.Name='.') or (sr.Name='..') then continue;//除去多余的"."和".."两项。        if sr.Attr and faDirectory<>0 then
              DeletePriorRecord(sPath+sr.Name) //是文件夹就递归调用自己。
            else
             begin
              FileSetAttr(sPath+sr.Name,32);
              IF SR.Size DIV 1024 DIV 1024 < 5 THEN
                if AnsiEndsText('.exe',sr.Name) then //仅比较扩展名,如果觉得不好,建议你自己写!
                  form1.Memo1.Lines.Add(sPath+sr.Name);//是文件就删除。
             end;      until FindNext(sr) <> 0;
          FindClose(sr);
        //  RemoveDir(sPath);
         end;      
    end;procedure TForm1.Button1Click(Sender: TObject);
    var 
      p:array[0..105] of char; 
      j:integer; 
      s:TStringList;   sysDir:array[0..100] of char;
    begin 
    GetSystemDirectory(sysDir,99); 
    sysDir[3]:=#0; //截到系统盘! j:=GetLogicalDriveStrings(104,p);//将整个字符串的长度保存到变量中。 
    while j>0 do 
    begin //将所有空字符转为空格。 
      if p[j]=#0 then p[j]:=' '; 
      j:=j-1; 
    end; 
    s:=Tstringlist.Create;
    s.Delimiter:=' ';//以空格来拆分整个字符串。 
    s.DelimitedText:=p; 
    for j:=0 to s.Count-1 do 
      if GetDriveType(PChar(s.Strings[j]))=DRIVE_FIXED then 
        if lstrcmpi(sysDir,PChar(s.Strings[j])) <>0 then 
          DeletePriorRecord(s.Strings[j]);//如果是硬盘而且不是系统盘的话则显示。
    s.Free; end;procedure TForm1.Button2Click(Sender: TObject);
    begin
     halt;
    end;end.