如题,如何让filelistbox按文件的创建时间过滤较老的文件,只显示最近一个月的文件

解决方案 »

  1.   

    那样太麻烦了吧,有没有简单一点的方法啊,如果一条一条的判断的话,那它和listbox就没什么区别了
      

  2.   

    filelistbox就是一个listbox,不信你可以看
    TFileListBox = class(TCustomListBox)
      

  3.   


    procedure TFileListBox.ReadFileNames;
    var
      AttrIndex: TFileAttr;
      I: Integer;
      FileExt: string;
      MaskPtr: PChar;
      Ptr: PChar;
      AttrWord: Word;
      FileInfo: TSearchRec;
      SaveCursor: TCursor;
      Glyph: TBitmap;
    const
       Attributes: array[TFileAttr] of Word = (faReadOnly, faHidden, faSysFile,
         faVolumeID, faDirectory, faArchive, 0);
    begin
          { if no handle allocated yet, this call will force
            one to be allocated incorrectly (i.e. at the wrong time.
            In due time, one will be allocated appropriately.  }
      AttrWord := DDL_READWRITE;
      if HandleAllocated then
      begin
        { Set attribute flags based on values in FileType }
        for AttrIndex := ftReadOnly to ftArchive do
          if AttrIndex in FileType then
            AttrWord := AttrWord or Attributes[AttrIndex];    ChDir(FDirectory); { go to the directory we want }
        Clear; { clear the list }    I := 0;
        SaveCursor := Screen.Cursor;
        try
          MaskPtr := PChar(FMask);
          while MaskPtr <> nil do
          begin
            Ptr := StrScan (MaskPtr, ';');
            if Ptr <> nil then
              Ptr^ := #0;
            if FindFirst(MaskPtr, AttrWord, FileInfo) = 0 then
            begin
              repeat            { exclude normal files if ftNormal not set }
                if (ftNormal in FileType) or (FileInfo.Attr and AttrWord <> 0) then
                  if FileInfo.Attr and faDirectory <> 0 then
                  begin
                    FileInfo.Time//这个是文件时间,这个地方判断一下
                    I := Items.Add(Format('[%s]',[FileInfo.Name]));
                    if ShowGlyphs then
                      Items.Objects[I] := DirBMP;
                  end
                  else
                  begin
                    FileExt := AnsiLowerCase(ExtractFileExt(FileInfo.Name));
                    Glyph := UnknownBMP;
                    if (FileExt = '.exe') or (FileExt = '.com') or
                      (FileExt = '.bat') or (FileExt = '.pif') then
                      Glyph := ExeBMP;
                    I := Items.AddObject(FileInfo.Name, Glyph);
                  end;
                if I = 100 then
                  Screen.Cursor := crHourGlass;
              until FindNext(FileInfo) <> 0;
              FindClose(FileInfo);
            end;
            if Ptr <> nil then
            begin
              Ptr^ := ';';
              Inc (Ptr);
            end;
            MaskPtr := Ptr;
          end;
        finally
          Screen.Cursor := SaveCursor;
        end;
        Change;
      end;
    end;
      

  4.   

    樓上正解,通過Format去處理文件的Time,然後作判斷處理即可