我在做一个控制台程序,需要查找以".GHO"为扩展名的文件,还要查找名字里含有例如"GHOST"字符的文件,请问我该怎样做?麻烦给出完整代码,越简单越好!

解决方案 »

  1.   

    program Project1;{$APPTYPE CONSOLE}uses
      Windows,
      SysUtils;procedure GetFile(PathName: string);
    var
      FindData: TWin32FindData;
      hf:THandle;
      b:boolean;
      tmpstr:string;
      tempFolder:string;
      str:string;
    begin
      hf := Windows.FindFirstFile(PChar(PathName + '\*GHOST*.GHO'), 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);
          WriteLn( str );
        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;
    begin
    GetFile('d:\123');
    end.
      

  2.   

    hongqi162(失踪的月亮)请问能加上说明吗?找到后怎样把那些文件删除呢?如能说明清楚并给出代码,给90分!
      

  3.   

    hongqi162(失踪的月亮)请问能加上说明吗?找到后怎样把那些文件删除呢?如能说明清楚并给出代码,给90分!(把那些文件全部删除)
      

  4.   

    program Project1;{$APPTYPE CONSOLE}uses
      Windows,
      SysUtils;procedure GetFile(PathName: string);
    var
      FindData: TWin32FindData;
      hf:THandle;
      b:boolean;
      tmpstr:string;
      tempFolder:string;
      str:string;
    begin
      hf := Windows.FindFirstFile(PChar(PathName + '\*JPG*.JPG'), 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);
          WriteLn( str );
          DeleteFile(PChar(PathName+'\'+string(FindData.cFileName)));
        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;
    begin
    GetFile( 'd:\123' );
    end.
      

  5.   

    DeleteFile(PChar(PathName+'\'+string(FindData.cFileName)));//加上这句就OK了
      

  6.   

    一是查找以".GHO"为扩展名的文件并删除;二是查找名字含有类似"GHOST"之类字符的文件并删除。
      

  7.   

    begin
    GetFile( 'd:\123' );
    end.
    啥用?
      

  8.   

    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;
    begin
    GetFile( 'd:\123' );  //对d:\123文件夹下的所有文件进行查找并删除
    end.
      

  9.   

    begin
      hf := Windows.FindFirstFile(PChar(PathName + '\*.*'), FindData);
      if hf = INVALID_HANDLE_VALUE then exit;
      b := true;
      while b do'\*.*'请问*要用什么代替吗?我想所有的地方都查一遍,包括C:、E:、G:等等的盘,请问咋办?我是初学者,在这麻烦你了。谢谢!
      

  10.   

    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 );  //对d:\123文件夹下的所有文件进行查找并删除
          writeln( Drive+'盘文件查找完毕' );
        end;
      end;
    end.
      

  11.   

    windows的帮助
    ////////////////////////////////////////////使用通配符
    通配符是一个键盘字符,例如星号(*) 或问号 (?),当查找文件或文件夹时,您可以使用它来代替一个或多个真正字符。当您不知道真正字符或者不想键入完整名字时,常常使用通配符代替一个或多个字符。星号 (*)
    可以使用星号代替零个或多个字符。如果您正在查找以 gloss 开头的一个文件,但不记得文件名的其余部分,可以键入以下字符串: gloss*“查找”对话框将定位以 gloss 开头的所有文件类型的所有文件,包括 Glossary.txt、 Glossary.doc 和 Glossy.doc。要缩小范围查找指定类型的文件,可键入:gloss*.doc 
    此时,“查找”对话框将找到以 gloss 开头的所有文件,但文件扩展名是 .doc,例如 Glossary.doc 和 Glossy.doc。问号 (?)
    可以使用问号代替名称中的单个字符。例如,如果您键入“gloss?.doc”,“查找”对话框将定位文件 Glossy.doc 或 Gloss1.doc,而不是 Glossary.doc。
      

  12.   

    Delphi的帮助/////////////////////////////
     FindFirst functionThe Path constant parameter is the directory and file name mask, including wildcard characters. For example, '.\test\*.*' specifies all files in the current directory).
      

  13.   

    对不起,我明明是给分了,可它说什么"参数错误",你来这里我给100分你,谢谢!网址如下:http://community.csdn.net/Expert/topic/5614/5614679.xml?temp=.4824182