简体   繁体   English

使用FileSystemWatcher监视目录

[英]Monitor a directory with FileSystemWatcher

I have a directory which is getting filled by some program. 我有一个正在被某些程序填充的目录。 I would like to monitor this directory and check if files are continuously getting added to the directory. 我想监视此目录,并检查文件是否不断添加到该目录中。 If for Example for 5 min no file is getting added to the direcotry I want to get notified. 如果例如5分钟,没有文件被添加到目录,我想得到通知。 The Files are also getting deleted but i just have to care about the new files. 文件也将被删除,但我只需要关心新文件。
The script would run for about 20 min every three hours. 该脚本每三个小时运行大约20分钟。
The code for the FileSystemWatcher: FileSystemWatcher的代码:

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\temp\test"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true

$created = Register-ObjectEvent $watcher "Created" -Action {
   write-host "Created: $($eventArgs.FullPath)"
   Get-Date -Format "HH:mm:ss" | Out-File "C:\temp\log.txt" -Append
}

I don't know how i could achive this, and I'm thankful for every input. 我不知道该如何做到这一点,我为我的每一次输入都感到感谢。

PowerShell scripts are not well suited to run continuously and report on events. PowerShell脚本不太适合连续运行并报告事件。

One way to achieve what you want is using a script like this: 一种实现所需目标的方法是使用如下脚本:

[DateTime] $LastWriteTime = (Get-ChildItem -Path "C:\temp\test" -File | Sort-Object LastWriteTime | Select -Last 1).LastWriteTime
if ($LastWriteTime -le (Get-Date).AddMinutes(-5))
{
    Write-Output "No files added in the last 5 minutes"
    # notify me...
} 

and schedule the script in Windows Task Scheduler to run every five minutes. 并安排Windows Task Scheduler中的脚本每五分钟运行一次。

It finds the newest file edited in the directory. 它会找到目录中编辑的最新文件。

No need for a FileSystemWatcher 无需FileSystemWatcher

You could have a separate script running via Task Scheduler which uses your log file to see if a file has been created in the last 5 minutes. 您可以通过Task Scheduler运行一个单独的脚本,该脚本使用您的日志文件查看最近5分钟内是否已创建文件。

$log = Get-Content -Path T:\log.txt 
$currentTime = Get-date
$5minutesAgo = $currentTime.AddMinutes(-5)
$FileAdded = $false

foreach($time in $log){
    if((get-date $time) -gt $5minutesAgo){
        $FileAdded = $true
    }
}

If($FileAdded) {
    Write-host "File was added in the last 5mins"
} Else {
    Write-host "No file added in the last 5mins"
    # Add in email code here
}

This will go through your log file line by line and check if the time was within the last file minutes. 这将逐行通过您的日志文件,并检查时间是否在最后文件分钟内。 Then it will set the variable $FileAdded to $true if there was a file added. 然后,如果添加了文件,它将把变量$FileAdded设置为$true You can then use this to decide if an email should be send or not. 然后,您可以使用它来决定是否发送电子邮件。

Note : If this is being run constantly make sure you roll over your log files regular to keep the processing down. 注意 :如果此程序连续运行,请确保定期将日志文件翻转以保持处理进度。 As you are not including a the date in your log file you would have to roll over your log file at least once a day. 由于您没有在日志文件中包含日期,因此您必须每天至少滚动一次日志文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM