关于每隔一定时间扫描文件夹的问题

sherlock303 2014-07-10 04:06:46
有个文件夹a,通过外部网络传输,会每隔一定时间,好比说30秒,生成一个文件。现在我想设计一个程序,每隔2分钟,扫描一次文件夹,统计新生成的文件信息。请问,这个每隔2分钟扫描一次可以怎样实现呢?除了用计时器触发之外还有没其他的办法?
...全文
1300 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 10 楼 sherlock303 的回复:
[quote=引用 6 楼 xiaoyao961 的回复:] 我有做过这种软件的多个商业工程
请问能不能给出一个FindNextChangeNotification的使用例子?[/quote] 像这种我也是研究了几天才做出来的,收点费用的话可以啊,QQ1058992075 最简单就是用DIR进行枚举,然后比较一下更新时间,这种会编程的人一般都能写,要用API或HOOK,或信号之类,或多线程,难度就是大了数十倍了
Tiger_Zhao 2014-07-14
  • 打赏
  • 举报
回复
FindNextChangeNotification 除了下木马、做病毒、或者安全软件,一般真的不需要这么实时的文件监控。
赵4老师 2014-07-14
  • 打赏
  • 举报
回复
参考 Microsoft SDK\samples\winbase\IO\fWatch\*.* 或 MSDN98\SAMPLES\VC98\SDK\WINBASE\IO\FWATCH\*.*
sherlock303 2014-07-12
  • 打赏
  • 举报
回复
引用 11 楼 Tiger_Zhao 的回复:
要是我告诉你我7楼的方案在金融系统中有不少案例,你是不是也要求例子了? 先理清自己的需求,想想哪个才是合适自己的。 要是选方案的标准就是“啊,这个有实际案例,这个好!” 那么就会“尼玛!为什么会发生这种问题?”
网上百度谷歌出来的用法大都是是c语言的。。。我想了解下在vb6里面怎样使用
sherlock303 2014-07-12
  • 打赏
  • 举报
回复
引用 11 楼 Tiger_Zhao 的回复:
要是我告诉你我7楼的方案在金融系统中有不少案例,你是不是也要求例子了? 先理清自己的需求,想想哪个才是合适自己的。 要是选方案的标准就是“啊,这个有实际案例,这个好!” 那么就会“尼玛!为什么会发生这种问题?”
阁下别激动。。。我只是想知道FindNextChangeNotification是怎样用法而已。。也不一定需要一个很具体的例子
inurlcn 2014-07-11
  • 打赏
  • 举报
回复
还有这么好用的控件 !!!
  • 打赏
  • 举报
回复
我有做过这种软件的多个商业工程
  • 打赏
  • 举报
回复
可以用多线程的方式,或者用API HOOK,或者监视文件夹变动的信号量,WaitForMultipleObjects(监视多个文件夹) ,WaitForSingleObject单个,用这种方法:FindNextChangeNotification
Tiger_Zhao 2014-07-11
  • 打赏
  • 举报
回复
你更加需要知道的是确定文件传输完毕吧?总不会看到有新文件就马上读取内容吧!

如果传入文件大小固定还好,文件达到指定长度就可以处理了。
如果文件大小可变,你需要对方再发送一个零长度的标志文件。比如看到数据文件为 01.dat 先不要处理,看到标志文件 01.end 才进行处理。

所以没必要用 fileSystemWatcher,直接开定时器扫描目录就可以了。
只需用 dir("a\*.end") 扫描标志文件。
处理完文件就把 .dat 文件移到历史目录(如果需要保留),.end 文件删除,这样接收目录仅有少量文件,不影响扫描速度。
sanshizi 2014-07-11
  • 打赏
  • 举报
回复
我觉得这是百度分词规则的一个bug
Tiger_Zhao 2014-07-11
  • 打赏
  • 举报
回复
要是我告诉你我7楼的方案在金融系统中有不少案例,你是不是也要求例子了?

先理清自己的需求,想想哪个才是合适自己的。
要是选方案的标准就是“啊,这个有实际案例,这个好!”
那么就会“尼玛!为什么会发生这种问题?”
sherlock303 2014-07-11
  • 打赏
  • 举报
回复
引用 6 楼 xiaoyao961 的回复:
我有做过这种软件的多个商业工程
请问能不能给出一个FindNextChangeNotification的使用例子?
赵4老师 2014-07-11
  • 打赏
  • 举报
回复
ReadDirectoryChangesW The ReadDirectoryChangesW function returns information describing the changes occurring within a directory. BOOL ReadDirectoryChangesW( HANDLE hDirectory, // handle to the directory to be watched LPVOID lpBuffer, // pointer to the buffer to receive the read // results DWORD nBufferLength, // length of lpBuffer BOOL bWatchSubtree, // flag for monitoring directory or // directory tree DWORD dwNotifyFilter, // filter conditions to watch for LPDWORD lpBytesReturned, // number of bytes returned LPOVERLAPPED lpOverlapped, // pointer to structure needed for // overlapped I/O LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine // pointer to completion routine ); Parameters hDirectory Handle to the directory to be watched. This directory must be opened with the FILE_LIST_DIRECTORY access right. lpBuffer Specifies the address of the formatted buffer in which the read results are to be returned. The structure of this buffer is defined by the FILE_NOTIFY_INFORMATION structure. This buffer is filled either synchronously or asynchronously, depending on how the directory is opened and what value is given to the lpOverlapped parameter. For more information, see the Remarks section. nBufferLength Specifies the length of the buffer pointed to by the lpBuffer parameter. bWatchSubtree Specifies whether the ReadDirectoryChangesW function will monitor the directory or the directory tree. If TRUE is specified, the function monitors the directory tree rooted at the specified directory. If FALSE is specified, the function monitors only the directory specified by the hDirectory parameter. dwNotifyFilter Specifies filter criteria the function checks to determine if the wait operation has completed. 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 file. 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_LAST_ACCESS Any change to the last access time of files in the watched directory or subtree causes a change notification wait operation to return. FILE_NOTIFY_CHANGE_CREATION Any change to the creation time of files in the watched directory or subtree causes a change notification wait operation to return. FILE_NOTIFY_CHANGE_SECURITY Any security-descriptor change in the watched directory or subtree causes a change notification wait operation to return. lpBytesReturned For synchronous calls, this parameter specifies the number of bytes transferred into the lpBuffer parameter. For asynchronous calls, this parameter is undefined. You must use an asynchronous notification technique to retrieve the number of bytes transferred. lpOverlapped Pointer to an OVERLAPPED structure that supplies data to be used during asynchronous operation. Otherwise, this value is NULL. The Offset and OffsetHigh members of this structure are not used. lpCompletionRoutine Pointer to a completion routine to be called when the operation has been completed or canceled and the calling thread is in an alertable wait state. For more information about this completion routine, see FileIOCompletionRoutine. Return Values If the function succeeds, the return value is nonzero. For synchronous calls, this means that the operation succeeded. For asynchronous calls, this indicates that the operation was successfully queued. If the function fails, the return value is zero. To get extended error information, call GetLastError. Remarks To obtain a handle to a directory, use the CreateFile function with FILE_FLAG_BACKUP_SEMANTICS as follows: hDir = CreateFile( DirName, // pointer to the file name FILE_LIST_DIRECTORY, // access (read-write) mode FILE_SHARE_READ|FILE_SHARE_DELETE, // share mode NULL, // security descriptor OPEN_EXISTING, // how to create FILE_FLAG_BACKUP_SEMANTICS, // file attributes NULL // file with attributes to copy ); A call to ReadDirectoryChangesW can be completed synchronously or asynchronously. To specify asynchronous completion, open the directory with CreateFile as shown above, but additionally specify the FILE_FLAG_OVERLAPPED attribute in the dwFlagsAndAttributes parameter. Then specify an OVERLAPPED structure when you call ReadDirectoryChangesW. Upon successful synchronous completion, the lpBuffer parameter is a formatted buffer and the number of bytes written to the buffer is available in lpBytesReturned. If the number of bytes transferred is zero, the buffer was too small to provide detailed information on all the changes that occurred in the directory or subtree. In this case, you should compute the changes by enumerating the directory or subtree. For asynchronous completion, you can receive notification in one of three ways: Using the GetOverlappedResult function. To receive notification through GetOverlappedResult, do not specify a completion routine in the lpCompletionRoutine parameter. Be sure to set the hEvent member of the OVERLAPPED structure to a unique event. Using the GetQueuedCompletionStatus function. To receive notification through GetQueuedCompletionStatus, do not specify a completion routine in lpCompletionRoutine. Associate the directory handle hDirectory with a completion port by calling the CreateIoCompletionPort function. Using a completion routine. To receive notification through a completion routine, do not associate the directory with a completion port. Specify a completion routine in lpCompletionRoutine. This routine is called whenever the operation has been completed or canceled while the thread is in an alertable wait state. The hEvent member of the OVERLAPPED structure is not used by the system, so you can use it yourself. QuickInfo Windows NT: Requires version 4.0 or later. Windows: Unsupported. Windows CE: Unsupported. Header: Declared in winbase.h. Import Library: Use kernel32.lib. See Also File I/O Overview, File Functions, CreateFile, CreateIoCompletionPort, FILE_NOTIFY_INFORMATION, FileIOCompletionRoutine, GetOverlappedResult, GetQueuedCompletionStatus, OVERLAPPED
threenewbee 2014-07-10
  • 打赏
  • 举报
回复
如果你用VB.NET可以直接使用FileSystemWatcher,是对这个API的封装。
tanta 2014-07-10
  • 打赏
  • 举报
回复
3、使用fileSystemWatcher,这个应该是最靠谱的。
threenewbee 2014-07-10
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/en-us/library/aa365465%28VS.85%29.aspx
tanta 2014-07-10
  • 打赏
  • 举报
回复
1、你可以用定时器 2、使用计划任务

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧