现在要删除指定目录下的指定文件?哪位有此源代码吗?感谢!

解决方案 »

  1.   

    用DeleteFile函数
    或者调用del命令
      

  2.   

    网上有很多例子
    http://www.programfan.com/blog/article.asp?id=2723
      

  3.   

    program Project1;{$APPTYPE CONSOLE}uses
      SysUtils, Windows;const
      Path = 'a\'; //要删除的目录路径
    var
      sr: TSearchRec;
      hFindFile: Cardinal;
      FileAttrs: Integer;
    begin
      FileAttrs:=faAnyFile;
      hFindFile:=FindFirst(Path+'*.*', FileAttrs, sr);
      if hFindFile = 0 then
      begin
        repeat
          if (sr.Attr and FileAttrs) = sr.Attr then
            if (sr.Name <> '.') and (sr.Name <> '..') then
            begin
              if (sr.Attr and $00000001) <> 0 then //如果是只读文件要先将它设置为非只读的,要不然删不掉
                if not SetFileAttributes(PChar(Path+sr.Name), FILE_ATTRIBUTE_NORMAL) then
                begin
                  WriteLn('Delete file faild! - '+sr.Name);
                  Continue;
                end;
              WriteLn('Delete file : '+sr.Name);
              DeleteFile(PChar(Path+sr.Name));
            end;
        until FindNext(sr) <> 0;
        FindClose(hFindFile);
      end;
      ReadLn;
    end.
      

  4.   

    function deldir(ADir:string):boolean;
    var
      FileStruct:TSHFileOpStruct;
    begin
      result:=false;
      if not directoryexists(ADir) then exit;  //源文件夹不存在
      if ADir[length(ADir)]='\' then
        delete(ADir,length(ADir),1);  FileStruct.Wnd :=0;
      FileStruct.wFunc :=FO_delete;
      FileStruct.pFrom:=Pchar(ADir);
      FileStruct.fFlags:=FOF_NOCONFIRMATION; //不需要提示.需要提示用FOF_ALLOWUNDO
      FileStruct.pTo :='';
      if SHFileOperation(FileStruct)=0 then
        result:=true
      else
        exit;
    end;
      

  5.   

    useSysUtilsprocedure delallfilesinpath(path:string);     //删除目录下*.*
    var
      sr:tsearchrec;
    begin
      if findfirst(path+'\*.*',faanyfile,sr)=0 then
      begin
         deletefile(path+'\'+sr.name);
      end ;
      while findnext(sr)=0 do
     begin
         deletefile(path+'\'+sr.name);
      end;
      FindClose(sr);  //释放FindFirst()时分配的内存
    end;
      

  6.   

    加上这句 来吧文件的属性设置为normal
     SetFileAttributes(PChar(filename), FILE_ATTRIBUTE_NORMAL)
      

  7.   

    To yq3woaini:
        如果目录下含有子目录(子目录又有文件)呢?
      

  8.   

    To  merkey2002(小样的):
       我不是想删除该目录,是想将目录下的所有文件进行删除!
      你那个方法我还想学习学习。请教!
      

  9.   

    ...
    这个你一定满意...
    如 yq3woaini所说,在deletefile前个 
    SetFileAttributes(PChar(filename), FILE_ATTRIBUTE_NORMAL),你自己改一下吧。
    function deldir2(ADir:string;bLeaveADir:boolean=false;bLeaveAllDir:boolean=false):boolean;
    var
      sr:tsearchrec;
      i:integer;
    begin
      result:=false;
      if not directoryexists(ADir) then exit;
      if ADir[length(ADir)]<>'\' then
        ADir:=ADir+'\';  //********对一个文件夹下所有文件的操作***********
      if findfirst(ADir+'*.*',faanyfile,sr)=0 then
        begin
          repeat
            if sr.Attr and fadirectory =0 then //是文件
              result:=deletefile(ADir+sr.Name)
            else if (sr.Name<>'.') and (sr.Name<>'..') then   //对本文件夹,上层文件夹不操作。
              result:=deldir2(ADir+sr.Name,bLeaveAllDir,bLeaveAllDir);
            if (sr.Name<>'.') and (sr.Name<>'..') and (result=false) then exit;
          until findnext(sr)<>0
        end;
      findclose(sr);
      //***************************
      if not bLeaveADir then
        result:=removedir(ADir);
    end;
      

  10.   

    这个方法尚有缺陷。
    1、类似'C:\Documents and Settings\Administrator\Local Settings\Temporary Internet Files'这样的目录便不能删除!
      

  11.   

    faanyfile是包括卷标的,这个也应该排除掉。
      

  12.   

    to  shxhark(泉) 
    我试了你说的目录也可以删除所有文件的
    wind2000下
      

  13.   

    WinExec(PAnsiChar('cmd /c rmdir /s /q '+Path1),sw_hide);
    sleep(500);
    之后再创建一个文件夹吧,呵呵
    mkdir(Path1);