关于监视文件更新的问题!(50分)

  • 主题发起人 主题发起人 hylwr
  • 开始时间 开始时间
H

hylwr

Unregistered / Unconfirmed
GUEST, unregistred user!
我想做一个和UpDate Now!差不多的软件(大家可能都用过这个软件吧),就
是监视本地目录下(包含子目录)的文件的更新,只把更新了的文件上传到
网站上,现在有以下一些问题,忘大伙能不吝赐教:
1.如何监视本地的文件更新,也就是一个遍历整个目录找到更新的文件?
2.Ftp上传主页功能如何制作,是不是要下载控件还是自己用Delphi写,那里有好的此类的免费控件?
 
1:监视本地的文件更新可以用读取文件的修改时间来判断。每次上传后你可以把文件
修改时间写入一个文件中,下一次上传时比较一下即可知道文件是否已更新。
2:Delphi自身就带有FTP控件,但据我所知速度比较慢,你要想写一个出众的FTP
软件的话,还是多啃几本网络编程的书吧,别把希望寄托在控件上。
 
1、基本与kucio相似。补充一点,每次系统开启后,系统从后台遍历所有文件的日期
这样才能随时掌握。
 
我不知道怎么遍历啊:(
 
就是访问你本地目录及其子目录下的所有文件,得到每一个文件得到修改的时间。
这里关键是知道遍历的算法。很多的数据结构的书上都有这种算法的。
找找看吧!

 
1. 获取当前目录下的所有下一级子目录,2. 存入字符串列表中(Tstrings)。
FindFirst是找出指定目录下第一个文件或目录。
FindNext一般和FindFirst配合使用,用来找出下一个文件或目录。
FindClose用来关闭查询。
3. 用FileExists函数查找当前目录,4. 寻找是否有满足条件的文件存在。
5. 依次使各个子目录成为当前目录,6. 递归调用本函数。
7. 释放资源,8. 返回查询结果。
代码如下:
1. 从搜索记录中判断是否是子目录。
function IsValidDir(SearchRec:TSearchRec):Boolean;
begin
if (SearchRec.Attr=16) and
(SearchRec.Name<>'.') and
(SearchRec.Name<>'..') then
Result:=True
else
Result:=False;
end;
2. 这是查询主体函数。

参数介绍:
Mainpath: 指定的查询目录。
Filename: 欲查询的文件。
Foundresult: 返回的含完整路径的匹配文件(可能有多个)。
如果有匹配文件,函数返回True,否则,返回False;

function SearchFile(mainpath:string;filename:string;
var foundresult:TStrings):Boolean;
var
i:integer;
Found:Boolean;
subdir1:TStrings;
searchRec:TsearchRec;
begin
found:=false;
if Trim(filename)<>'' then
begin
subdir1:=TStringList.Create;//字符串列表必须动态生成
//找出所有下级子目录。
if (FindFirst(mainpath+'*.*', faDirectory, SearchRec)=0) then
begin
if IsValidDir(SearchRec) then
subdir1.Add(SearchRec.Name);
while (FindNext(SearchRec) = 0) do
begin
if IsValidDir(SearchRec) then
subdir1.Add(SearchRec.Name);
end;
end;
FindClose(SearchRec);
//查找当前目录。
if FileExists(mainpath+filename) then
begin
found:=true;
foundresult.Add(mainpath+filename);
end;

//这是递归部分,查找各子目录。
for i:=0 to subdir1.Count-1 do
found:=Searchfile(mainpath+subdir1.Strings+
'/',Filename,foundresult)or found;
//资源释放并返回结果。
subdir1.Free;
end;
result:=found;
end;
 
The FindFirstChangeNotification function creates a change notification handle and sets up initial change notification filter conditions. A wait on a notification handle succeeds when a change matching the filter conditions occurs in the specified directory or subtree.

HANDLE FindFirstChangeNotification(

LPCTSTR lpPathName, // pointer to name of directory to watch
BOOL bWatchSubtree, // flag for monitoring directory or directory tree
DWORD dwNotifyFilter // filter conditions to watch for
);


Parameters

lpPathName

Points to a null-terminated string that specifies the path of the directory to watch.

bWatchSubtree

Specifies whether the function will monitor the directory or the directory tree. If this parameter is TRUE, the function monitors the directory tree rooted at the specified directory; if it is FALSE, it monitors only the specified directory.

dwNotifyFilter

Specifies the filter conditions that satisfy a change notification wait. This parameter can be one or more of the following values:

Value Meaning
FILE_NOTIFY_CHANGE_FILE_NAME
Any filename change in the watched directory or subtree causes a change notification wait operation to return. Changes include renaming, creating, or deleting a filename.
FILE_NOTIFY_CHANGE_DIR_NAME
Any directory-name change in the watched directory or subtree causes a change notification wait operation to return. Changes include creating or deleting a directory.
FILE_NOTIFY_CHANGE_ATTRIBUTES
Any attribute change in the watched directory or subtree causes a change notification wait operation to return.
FILE_NOTIFY_CHANGE_SIZE
Any file-size change in the watched directory or subtree causes a change notification wait operation to return. The operating system detects a change in file size only when the file is written to the disk. For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed.
FILE_NOTIFY_CHANGE_LAST_WRITE
Any change to the last write-time of files in the watched directory or subtree causes a change notification wait operation to return. The operating system detects a change to the last write-time only when the file is written to the disk. For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed.
FILE_NOTIFY_CHANGE_SECURITY
Any security-descriptor change in the watched directory or subtree causes a change notification wait operation to return.


Return Values

If the function succeeds, the return value is a handle to a find change notification object.
If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.

Remarks

The wait functions can monitor the specified directory or subtree by using the handle returned by the FindFirstChangeNotification function. A wait is satisfied when one of the filter conditions occurs in the monitored directory or subtree.
After the wait has been satisfied, the application can respond to this condition and continue monitoring the directory by calling the
FindNextChangeNotification function and the appropriate wait function. When the handle is no longer needed, it can be closed by using the FindCloseChangeNotification function.

See Also

FindCloseChangeNotification, FindNextChangeNotification
 
如果时钟坏了,岂非...
不是有个A属性,只要是修改过了,肯定会有这个属性,传完了再去掉A属性就可以吗。
 
hubdog(对你表示崇高的敬意,您的发言让我感到我们所要做的仅仅是补遗了)
 
to energy:
别介呀,我这是从我的葵花宝典里摘抄的一段.你当我有闲工夫写这个
对了,明后天我要提一个用delphi写Cookie出现的问题,一定要积极抢答呀。:)
 
葵花宝典?使得使得,我要!我还是童子之身,练习应该没问题吧。
介绍一下。
 
Liu JZX提供的信息很有帮助
最近我也想解决这个问题
下面使用VC写的一小段,
void CDynamicView::OnButton32771()
{
HANDLE hHandle = FindFirstChangeNotification("c:", TRUE,
FILE_NOTIFY_CHANGE_FILE_NAME);
DWORD Result = WaitForSingleObject(hHandle, 20000);
switch (Result)
{
case WAIT_ABANDONED:
MessageBox("Wait Abandoned!", NULL, 0);
break;
case WAIT_OBJECT_0:
MessageBox("Wait Signaled!", NULL, 0);
break;
case WAIT_TIMEOUT:
MessageBox("Wait Timeout!", NULL, 0);
break;
}
}
 
补充一下,WAIT_OBJECT_0发生在C:下有文件被改了名字的时候.
但具体哪个文件被改动了我还没有试出来.
但最笨的办法是为C:下每个文件设置一个Waiting object
 
接受答案了.
 
后退
顶部