如何取得一个文件夹下的所有文件的名字,还有如何删除文件夹里的所有文件

解决方案 »

  1.   

    findfirst,findnext查找,deletefile删除
      

  2.   

    function DeletePath(mDirName: string): Boolean; { 返回删除指定目录是否成功 }
    var
      vSearchRec: TSearchRec;
      vPathName: string;
      K: Integer;
    begin
      Result := True;
      vPathName := mDirName + '\*.*';
      K := FindFirst(vPathName, faAnyFile, vSearchRec);
      while K = 0 do begin
        if (vSearchRec.Attr and faDirectory > 0) and
          (Pos(vSearchRec.Name, '..') = 0) then begin
          FileSetAttr(mDirName + '\' + vSearchRec.Name, faDirectory);
          Result := DeletePath(mDirName + '\' + vSearchRec.Name);
        end else if Pos(vSearchRec.Name, '..') = 0 then begin
          FileSetAttr(mDirName + '\' + vSearchRec.Name, 0);
          Result := DeleteFile(PChar(mDirName + '\' + vSearchRec.Name));
        end;
        if not Result then Break;
        K := FindNext(vSearchRec);
      end;
      FindClose(vSearchRec);
      Result := RemoveDir(mDirName);
    end; { DeletePath }
    =============
    取得一个文件夹下的所有文件的名字只要把上述函数中有关删除的部分修改一下就可以了
      

  3.   

    还有一个本方法就是使用一个filelist,但是不现实,然后给这个filelist一个路径,遍历这个filelist,得到了全部文件名,但是不能找子目录,还有使用find的时候要注意区分文件和文件夹。
      

  4.   

    查找文件的.....注意参数:)
    procedure findall(disk,path: String; var fileresult: Tstrings);
    var
    fpath: String;
    fs: TsearchRec;
    begin
    fpath:=disk+path+'\*.*';
    if findfirst(fpath,faAnyFile,fs)=0 then
    begin
    if (fs.Name<>'.')and(fs.Name<>'..') then
    if (fs.Attr and faDirectory)=faDirectory then
    findall(disk,path+'\'+fs.Name,fileresult)
    else
    fileresult.add(disk+strpas(strupper(pchar(path)))+'\'+strpas(
    strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')');
    while findnext(fs)=0 do
    begin
    if (fs.Name<>'.')and(fs.Name<>'..') then
    if (fs.Attr and faDirectory)=faDirectory then
    findall(disk,path+'\'+fs.Name,fileresult)
    else
    fileresult.add(disk+strpas(strupper(pchar(path)))+'\'+str
    pas(strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')');
    end;
    end;
    findclose(fs);
    end;
      

  5.   

    删除目录
    删除目录:function DelDirectory(const Source:string): boolean;
    var
      fo: TSHFILEOPSTRUCT;
    begin
      FillChar(fo, SizeOf(fo), 0);
      with fo do
      begin
        Wnd := 0;
        wFunc := FO_DELETE;
        pFrom := PChar(source+#0);
        pTo := #0#0;
        fFlags := FOF_NOCONFIRMATION+FOF_SILENT;
      end;
      Result := (SHFileOperation(fo) = 0);
    end;
    复制目录:
    ///复制Source整个目录到DEST目录,如果Dest不存在,自动建立,如果DEST存在,那么Source将作为Dest的子目录!
    //例如如果要复制E:\Temp整个目录到E:\那么代码为: copydirectory('e:\temp','e:\');
    ///如果要复制E:\Temp到E:\Test目录下面,那么代码为:CopyDirecotry('E:\Temp','E:\TEST');
    function CopyDirectory(const Source, Dest: string): boolean;
    var
      fo: TSHFILEOPSTRUCT;
    begin
      FillChar(fo, SizeOf(fo), 0);
      with fo do
      begin
        Wnd := 0;
        wFunc := FO_COPY;
        pFrom := PChar(source+#0);
        pTo := PChar(Dest+#0);
        fFlags := FOF_NOCONFIRMATION+FOF_NOCONFIRMMKDIR    ;
      end;
      Result := (SHFileOperation(fo) = 0);
    end;
     
      

  6.   

    DelTree命令uses FileCtrl;  procedure DelTree(const Directory: TFileName);
      var
        DrivesPathsBuff: array[0..1024] of char;
        DrivesPaths: string;
        len: longword;
        ShortPath: array[0..MAX_PATH] of char;
        dir: TFileName;
      procedure rDelTree(const Directory: TFileName);
      var
        SearchRec: TSearchRec;
        Attributes: LongWord;
        ShortName, FullName: TFileName;
        pname: pchar;
      begin
        if FindFirst(Directory + '*', faAnyFile and not faVolumeID,
          SearchRec) = 0 then begin
          try
            repeat // 检测所有的文件和目录
              if SearchRec.FindData.cAlternateFileName[0] = #0 then
                ShortName := SearchRec.Name
              else
                ShortName := SearchRec.FindData.cAlternateFileName;
              FullName := Directory + ShortName;
              if (SearchRec.Attr and faDirectory) <> 0 then begin
                // 是一个目录
                if (ShortName <> '.') and (ShortName <> '..') then
                  rDelTree(FullName + '\');
              end else begin
                // 是一个文件
                pname := PChar(FullName);
                Attributes := GetFileAttributes(pname);
                if Attributes = $FFFFFFFF then
                  raise EInOutError.Create(SysErrorMessage(GetLastError));
                if (Attributes and FILE_ATTRIBUTE_READONLY) <> 0 then
                  SetFileAttributes(pname, Attributes and not
                    FILE_ATTRIBUTE_READONLY);
                if Windows.DeleteFile(pname) = False then
                  raise EInOutError.Create(SysErrorMessage(GetLastError));
              end;
            until FindNext(SearchRec) <> 0;
          except
            FindClose(SearchRec);
            raise;
          end;
          FindClose(SearchRec);
        end;
        if Pos(#0 + Directory + #0, DrivesPaths) = 0 then begin
          // 如果不是根目录,就删除
          pname := PChar(Directory);
          Attributes := GetFileAttributes(pname);
          if Attributes = $FFFFFFFF then
            raise EInOutError.Create(SysErrorMessage(GetLastError));
          if (Attributes and FILE_ATTRIBUTE_READONLY) <> 0 then
            SetFileAttributes(pname, Attributes and not
              FILE_ATTRIBUTE_READONLY);
          if Windows.RemoveDirectory(pname) = False then begin
            raise EInOutError.Create(SysErrorMessage(GetLastError));
          end;
        end;
      end;
      // ----------------
      begin
        DrivesPathsBuff[0] := #0;
        len := GetLogicalDriveStrings(1022, @DrivesPathsBuff[1]);
        if len = 0 then
          raise EInOutError.Create(SysErrorMessage(GetLastError));
        SetString(DrivesPaths, DrivesPathsBuff, len + 1);
        DrivesPaths := Uppercase(DrivesPaths);
        len := GetShortPathName(PChar(Directory), ShortPath, MAX_PATH);
        if len = 0 then
          raise EInOutError.Create(SysErrorMessage(GetLastError));
        SetString(dir, ShortPath, len);
        dir := Uppercase(dir);
        rDelTree(IncludeTrailingBackslash(dir));
      end;
      

  7.   

    //强制删除文件夹的函数
    //uses FileCtrl;
      procedure DelTree(const Directory: TFileName); 
      var 
        DrivesPathsBuff: array[0..1024] of char; 
        DrivesPaths: string; 
        len: longword; 
        ShortPath: array[0..MAX_PATH] of char; 
        dir: TFileName; 
      procedure rDelTree(const Directory: TFileName); 
      // Recursively deletes all files and directories 
      // inside the directory passed as parameter. 
      var 
        SearchRec: TSearchRec; 
        Attributes: LongWord; 
        ShortName, FullName: TFileName; 
        pname: pchar; 
      begin 
        if FindFirst(Directory + '*', faAnyFile and not faVolumeID, 
           SearchRec) = 0 then begin 
          try 
            repeat // Processes all files and directories 
              if SearchRec.FindData.cAlternateFileName[0] = #0 then 
                ShortName := SearchRec.Name 
              else 
                ShortName := SearchRec.FindData.cAlternateFileName; 
              FullName := Directory + ShortName; 
              if (SearchRec.Attr and faDirectory) <> 0 then begin 
                // It's a directory 
                if (ShortName <> '.') and (ShortName <> '..') then 
                  rDelTree(FullName + '\'); 
              end else begin 
                // It's a file 
                pname := PChar(FullName); 
                Attributes := GetFileAttributes(pname); 
                if Attributes = $FFFFFFFF then 
                  raise EInOutError.Create(SysErrorMessage(GetLastError)); 
                if (Attributes and FILE_ATTRIBUTE_READONLY) <> 0 then 
                  SetFileAttributes(pname, Attributes and not 
                    FILE_ATTRIBUTE_READONLY); 
                if Windows.DeleteFile(pname) = False then 
                  raise EInOutError.Create(SysErrorMessage(GetLastError)); 
              end; 
            until FindNext(SearchRec) <> 0; 
          except 
            FindClose(SearchRec); 
            raise; 
          end; 
          FindClose(SearchRec); 
        end; 
        if Pos(#0 + Directory + #0, DrivesPaths) = 0 then begin 
          // if not a root directory, remove it 
          pname := PChar(Directory); 
          Attributes := GetFileAttributes(pname); 
          if Attributes = $FFFFFFFF then 
            raise EInOutError.Create(SysErrorMessage(GetLastError)); 
          if (Attributes and FILE_ATTRIBUTE_READONLY) <> 0 then 
            SetFileAttributes(pname, Attributes and not 
              FILE_ATTRIBUTE_READONLY); 
          if Windows.RemoveDirectory(pname) = False then begin 
            raise EInOutError.Create(SysErrorMessage(GetLastError)); 
          end; 
        end; 
      end; 
      // ---------------- 
      begin 
        DrivesPathsBuff[0] := #0; 
        len := GetLogicalDriveStrings(1022, @DrivesPathsBuff[1]); 
        if len = 0 then 
          raise EInOutError.Create(SysErrorMessage(GetLastError)); 
        SetString(DrivesPaths, DrivesPathsBuff, len + 1); 
        DrivesPaths := Uppercase(DrivesPaths); 
        len := GetShortPathName(PChar(Directory), ShortPath, MAX_PATH); 
        if len = 0 then 
          raise EInOutError.Create(SysErrorMessage(GetLastError)); 
        SetString(dir, ShortPath, len); 
        dir := Uppercase(dir); 
        rDelTree(IncludeTrailingBackslash(dir)); 
      end; 
      

  8.   

    以上procedures or functions 转帖自  Delphi所有资料
      

  9.   

    我还有一个Deltree的函数,只有5行代码,不知你要不要。
      

  10.   

    //得到某一文件夹下所有文件列表的函数 /////////////////////////////////////
    // getFileListFromDir('c:\windows',false,fileList);
                                //c:\windows or c:      ture|false
    procedure getFileListFromDir(dir:string;includeSubDir:boolean;var fileList:TStringList);
    var
      sr:TSearchRec;
      path:String;
    begin
      path:=dir+'\';      //c:\windows\
      dir:=dir+'\*.*';    //c:\windows\*.*
      if findFirst(dir,faanyfile,sr)=0 then
      repeat
      begin
        if (sr.Name<>'.') and (sr.Name<>'..') and ((fileGetAttr(path+sr.Name) and faDirectory)<>faDirectory) then
          fileList.Add(path+sr.Name);
        if (includeSubDir) and (sr.Name<>'.') and (sr.Name<>'..') and ((fileGetAttr(path+sr.Name) and faDirectory)=faDirectory) then
          getFileListFromDir(path+sr.Name,includeSubDir,fileList);
      end;
      until findNext(sr)<>0;
      findClose(sr);
    end;