请问:如何获取某个文件夹下最新创建(根据文件创建日期断定)的文件名称(100分)

  • 主题发起人 主题发起人 myhby
  • 开始时间 开始时间
通过TSearchRec FindFirst FindNext 查找 判断TSearchRec.Time
 
把文件夹下所有的文件都枚举一遍,然后找出最新的日期.比如,用现在的时间减每个文件的创建日期,最小值就是最新的.
 
下面这个是找文件的,如果想找日期最新的大同小意了....
大概是这样的用GetFileTime函数来得到日期,再通过FileDateToDateTime,后对日期进行比较...就行了..
procedure TForm1.findall(Disk,path:string;var Fileresult:TStringList);
var
lsPath :string;
fs :TsearchRec;
begin
lsPath:=Disk+path+'/*.*';
if findfirst(lsPath,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 if lowercase(ExtractFileExt(fs.Name))='.exe' then
Fileresult.add(Disk+strpas(strupper(pchar(path)))+'/'+strpas(strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')');
end;
end;
findclose(fs);
end;
 
查找文件,按照时间排序啊
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
Path: String;
SearchRec: TSearchRec;
FirstDateTime: TDateTime;
FirstFileName: String;
begin
FirstDateTime := now;
Path := Edit1.Text;
if FindFirst(Path, faAnyFile, SearchRec) = 0 then
begin
if FileDateToDateTime(SearchRec.Time) < FirstDateTime then
begin
FirstFileName := Path + SearchRec.Name;
FirstDateTime := FileDateToDateTime(SearchRec.Time);
end;
end;
while (FindNext(SearchRec) = 0) do
begin
if FileDateToDateTime(SearchRec.Time) < FirstDateTime then
begin
FirstFileName := Path + SearchRec.Name;
FirstDateTime := FileDateToDateTime(SearchRec.Time);
end;
end;
FindClose(SearchRec);
Edit2.Text := FirstFileName + DateTimeToStr(FirstDateTime);
end;

end.

代码中用到了2个edit, edit1是用来输入文件夹的完整路径的,edit2是给出结果的
 
上面的都没问题,但要找子文件夹因该用递归
 
楼上都说的比较全了,枚举,递归,比较日期,保留最新的文件名和日期做比较。
 
function TForm1.Deltree(Path: string): Boolean;
var
SearchRec: TSearchRec;
OldDir: string;
begin
if DirectoryExists(Path) then
begin
OldDir := GetCurrentDir;
ChDir(Path);
FindFirst(' . ', faAnyFile, SearchRec);
repeat
FileSetAttr(SearchRec.name, 0);
if (SearchRec.Attr and faDirectory > 0) then
begin
if (SearchRec.name[1] <> '.') then
if (not Deltree(SearchRec.name)) then
Break;
end
else
if not DeleteFile(SearchRec.name) then
Break;
until (FindNext(SearchRec) <> 0);
ChDir('..');
Result := RemoveDir(Path);
SetCurrentDir(OldDir);
end
else
Result := False;
end;
 
不要用 Delphi 的 FindFirst

用 Win32 FindFirstFile
 
新建的容易,可以通过系统消息获得:
const S_CMD_Modify1='更新目录';
S_CMD_Modify2='更新文件 文件名:';
S_CMD_CreateFile='建立文件 文件名:';
private
procedure WMShellReg(var Message:TMessage);message WM_SHNOTIFY;

procedure TForm2.WMShellReg(var Message:TMessage);
//系统消息处理函数
var S,strPath1,strPath2:String;
charPath:array[0..259]of char;
pidlItem:PSHNOTIFYSTRUCT;
begin
pidlItem:=PSHNOTIFYSTRUCT(Message.wParam);
//获得系统消息相关得路径
SHGetPathFromIDList(pidlItem.dwItem1,charPath);
strPath1:=charPath;
SHGetPathFromIDList(pidlItem.dwItem2,charPath);
strPath2:=charPath;
S:=SHEvEntName(strPath1,strPath2,Message.lParam);
if Copy(S,1,Length(S_CMD_CreateFile))=S_CMD_CreateFile then begin
S:=Trim(Copy(S,Length(S_CMD_CreateFile)+1,Length(S)));
ShellChangeNotifierChangeNew(S,True);
end else if Copy(S,1,Length(S_CMD_Modify1))=S_CMD_Modify1 then begin
S:=Trim(Copy(S,Length(S_CMD_Modify1)+1,Length(S)));
ShellChangeNotifierChangeNew(S,False);
end else if Copy(S,1,Length(S_CMD_Modify2))=S_CMD_Modify1 then begin
S:=Trim(Copy(S,Length(S_CMD_Modify2)+1,Length(S)));
ShellChangeNotifierChangeNew(S,False);
end;
end;
unit PublicDir;

interface
uses SysUtils,StrUtils,Classes,Windows,Graphics,ComCtrls,
DateUtils,IniFiles,StdCtrls,ShlObj,ShellCtrls,Forms,
Messages,Activex;

const
SHCNE_RENAMEITEM = $1; SHCNE_CREATE = $2; SHCNE_DELETE = $4; SHCNE_MKDIR = $8;
SHCNE_RMDIR = $10; SHCNE_MEDIAINSERTED = $20; SHCNE_MEDIAREMOVED = $40;
SHCNE_DRIVEREMOVED = $80; SHCNE_DRIVEADD = $100; SHCNE_NETSHARE = $200;
SHCNE_NETUNSHARE = $400; SHCNE_ATTRIBUTES = $800; SHCNE_UPDATEDIR = $1000;
SHCNE_UPDATEITEM = $2000; SHCNE_SERVERDISCONNECT = $4000;
SHCNE_UPDATEIMAGE = $8000; SHCNE_DRIVEADDGUI = $10000;
SHCNE_RENAMEFOLDER = $20000; SHCNE_FREESPACE = $40000; SHCNE_ASSOCCHANGED = $8000000;
SHCNE_DISKEVENTS = $2381F; SHCNE_GLOBALEVENTS = $C0581E0;
SHCNE_ALLEVENTS = $7FFFFFFF; SHCNE_INTERRUPT = $80000000;
SHCNF_IDLIST = 0; // LPITEMIDLIST
SHCNF_PATHA = $1; // path name
SHCNF_PRINTERA = $2; // printer friendly name
SHCNF_DWORD = $3; // DWORD
SHCNF_PATHW = $5; // path name
SHCNF_PRINTERW = $6; // printer friendly name
SHCNF_TYPE = $FF; SHCNF_FLUSH = $1000; SHCNF_FLUSHNOWAIT = $2000;
SHCNF_PATH = SHCNF_PATHW; SHCNF_PRINTER = SHCNF_PRINTERW;
WM_SHNOTIFY = $401; NOERROR = 0;

type
PSHNOTIFYSTRUCT=^SHNOTIFYSTRUCT;
SHNOTIFYSTRUCT = record
dwItem1 : PItemIDList;
dwItem2 : PItemIDList;
end;
Type
PSHFileInfoByte=^SHFileInfoByte;
_SHFileInfoByte = record
hIcon :Integer;
iIcon :Integer;
dwAttributes : Integer;
szDisplayName : array [0..259] of char;
szTypeName : array [0..79] of char;
end;
SHFileInfoByte=_SHFileInfoByte;
Type
PIDLSTRUCT = ^IDLSTRUCT;
_IDLSTRUCT = record
pidl : PItemIDList;
bWatchSubFolders : Integer;
end;
IDLSTRUCT =_IDLSTRUCT;
function SHNotify_Register(hWnd : Integer) : Bool;
function SHNotify_UnRegister:Bool;
function SHEventName(strPath1,strPath2:string;lParam:Integer):string;
Function SHChangeNotifyDeregister(hNotify:integer):integer;stdcall; external 'Shell32.dll' index 4;
Function SHChangeNotifyRegister(hWnd,uFlags,dwEventID,uMSG,cItems:LongWord; lpps:PIDLSTRUCT):integer;stdcall;external 'Shell32.dll' index 2;
Function SHGetFileInfoPidl(pidl:PItemIDList; dwFileAttributes : Integer; psfib : PSHFILEINFOBYTE;
cbFileInfo : Integer; uFlags : Integer):Integer;stdcall; external 'Shell32.dll' name 'SHGetFileInfoA';

var
m_hSHNotify:Integer;
m_pidlDesktop : PItemIDList;

implementation

function SHEventName(strPath1,strPath2:string;lParam:Integer):string;
var sEvent:String;
begin
case lParam of //根据参数设置提示消息
SHCNE_RENAMEITEM: sEvent := '重命名文件'+strPath1+'为'+strpath2;
SHCNE_CREATE: sEvent := '建立文件 文件名:'+strPath1;
SHCNE_DELETE: sEvent := '删除文件 文件名:'+strPath1;
SHCNE_MKDIR: sEvent := '新建目录 目录名:'+strPath1;
SHCNE_RMDIR: sEvent := '删除目录 目录名:'+strPath1;
SHCNE_MEDIAINSERTED: sEvent := strPath1+'中插入可移动存储介质';
SHCNE_MEDIAREMOVED: sEvent := strPath1+'中移去可移动存储介质'+strPath1+' '+strpath2;
SHCNE_DRIVEREMOVED: sEvent := '移去驱动器'+strPath1;
SHCNE_DRIVEADD: sEvent := '添加驱动器'+strPath1;
SHCNE_NETSHARE: sEvent := '改变目录'+strPath1+'的共享属性';
SHCNE_ATTRIBUTES: sEvent := '改变文件目录属性 文件名'+strPath1;
SHCNE_UPDATEDIR: sEvent := '更新目录'+strPath1;
SHCNE_UPDATEITEM: sEvent := '更新文件 文件名:'+strPath1;
SHCNE_SERVERDISCONNECT: sEvent := '断开与服务器的连接'+strPath1+' '+strpath2;
SHCNE_UPDATEIMAGE: sEvent := 'SHCNE_UPDATEIMAGE';
SHCNE_DRIVEADDGUI: sEvent := 'SHCNE_DRIVEADDGUI';
SHCNE_RENAMEFOLDER: sEvent := '重命名文件夹'+strPath1+'为'+strpath2;
SHCNE_FREESPACE: sEvent := '磁盘空间大小改变';
SHCNE_ASSOCCHANGED: sEvent := '改变文件关联';
else sEvent:='未知操作'+IntToStr(lParam);
end;
Result:=sEvent;
end;

function SHNotify_Register(hWnd : Integer) : Bool;
var ps:PIDLSTRUCT;
begin
{$R-}
Result:=False;
new(ps);
If m_hSHNotify = 0 then
begin //获取桌面文件夹的Pidl
if SHGetSpecialFolderLocation(0,CSIDL_DESKTOP,m_pidlDesktop)<> NOERROR then
exit;
if Boolean(m_pidlDesktop) then begin
ps.bWatchSubFolders := 1;
ps.pidl := m_pidlDesktop;// 利用SHChangeNotifyRegister函数注册系统消息处理
m_hSHNotify := SHChangeNotifyRegister(hWnd,(SHCNF_TYPE Or SHCNF_IDLIST),(SHCNE_ALLEVENTS Or SHCNE_INTERRUPT),WM_SHNOTIFY,1,ps);
Result := Boolean(m_hSHNotify);
end Else// 如果出现错误就使用 CoTaskMemFree函数来释放句柄
CoTaskMemFree(m_pidlDesktop);
dispose(ps);
End;
{$R+}
end;

function SHNotify_UnRegister:Bool;
begin
Result:=False;
If Boolean(m_hSHNotify) then //取消系统消息监视,同时释放桌面的Pidl
If Boolean(SHChangeNotifyDeregister(m_hSHNotify)) Then begin
{$R-}
m_hSHNotify := 0;
CoTaskMemFree(m_pidlDesktop);
Result := True;
{$R-}
End;
end;

end.
 
请问楼上的朋友:
文件的重命名,移动和删除,复制怎么监控?
 
后退
顶部