如何检测文件夹中是否添加了任何文件? [英] How to detect if there is any file added in a folder?

查看:108
本文介绍了如何检测文件夹中是否添加了任何文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以检测文件夹中是否添加了任何文件?包括子文件夹.

Is it a way to detect if there is any file added in a folder? Include the sub-folder.

例如,检查c:\data-files\ 文件夹或其子文件夹中是否添加了文本文件*.txt.

For example, check if any text file *.txt is added in folder c:\data-files\ or its sub-folders.

该文件夹也可以是其他机器的共享文件夹.

The folder can be shared folder of another machine too.

推荐答案

也许您对触发的事件类型感到困惑:http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher_events(v=vs.110).aspx

Perhaps you are confused on the types of events that are triggered: http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher_events(v=vs.110).aspx

这应该可以工作,取自上面的链接并根据您的要求进行修改:

This should work, taken from the link above and modified for your requirements:

#By BigTeddy 05 September 2011 

#This script uses the .NET FileSystemWatcher class to monitor file events in folder(s). 
#The advantage of this method over using WMI eventing is that this can monitor sub-folders. 
#The -Action parameter can contain any valid Powershell commands.  I have just included two for example. 
#The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true. 
#You need not subscribe to all three types of event.  All three are shown for example. 
# Version 1.1 

$folder = '\\remote\shared' # Enter the root path you want to monitor. 
$filter = '*.txt'  # You can enter a wildcard filter here. 

# In the following line, you can change 'IncludeSubdirectories to $true if required.                           
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'} 

# Here, all three events are registerd.  You need only subscribe to events that you need: 

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 
$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$timeStamp = $Event.TimeGenerated 
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green 
Out-File -FilePath c:\scripts\filechange\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"} 

请注意,一旦您关闭 powershell 控制台,fileSystemWatcher 就会被丢弃,并且将不再监视文件夹.因此,您必须确保 powershell 窗口保持打开状态.为了做到这一点而不会妨碍您,我建议执行计划任务 http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/12/use-scheduled-tasks-to-run-powershell-commands-on-windows.aspx

Please note that once you close the powershell console the fileSystemWatcher is thrown away, and will no longer monitor the folder(s). So you have to make sure the powershell window stays open. In order to do that without it getting in your way I suggest a scheduled task http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/12/use-scheduled-tasks-to-run-powershell-commands-on-windows.aspx

这篇关于如何检测文件夹中是否添加了任何文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆