我正在尝试使用Powershell创建文件监视器。一旦文件被更改,就需要它运行我的PHP脚本。我的第一个想法是,一切都很完美,但似乎只是触发了一次。
一旦我更改了给定文件夹中的* .js文件,PHP脚本就会触发。但它只触发一次。几分钟后我再次更改了文件,但php脚本不再执行(即使Write-Host
没有任何内容)。
这是我的Powershell-Script:
#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
$global:folder = 'C:\Users\Dario\Desktop\scripts' # Enter the root path you want to monitor.
$filter = '*.js' # 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 = $false;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
}
Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
$path = $global:folder + "\" + $name
Try {
Write-Host "The file '$name' was $changeType at $timeStamp" -fore white
cmd.exe /k ""C:\Users\Dario\Desktop\mep\script.bat" "$path""
} Catch {
Write-Host "Problems"
}
}
# To stop the monitoring, run the following commands:
# Unregister-Event FileDeleted
# Unregister-Event FileCreated
# Unregister-Event FileChanged
答案 0 :(得分:1)
答案 1 :(得分:0)
我不想粗鲁,但我有另一种解决方案。我在IntelliJ上开发。 IntelliJ有一个自己的File Watcher
,这就是我使用这个的原因。