gpt4 book ai didi

php - 使用 Powershell 的文件观察器并不总是触发

转载 作者:搜寻专家 更新时间:2023-10-31 21:25:55 24 4
gpt4 key购买 nike

我正在尝试使用 Powershell 创建一个文件观察器。需要它在文件更改后立即运行我的 PHP 脚本。我的第一个想法是一切正常,但似乎只触发一次。

一旦我更改给定文件夹中的 *.js 文件,PHP 脚本就会触发。但它只会触发一次。几分钟后我再次更改文件,但是 php 脚本不再执行(甚至 Write-Host 也没有执行任何操作)。

这是我的 Powershell 脚本:

#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

最佳答案

有趣的问题!我为此开始谷歌搜索并找到了 this为它准备好的功能:

Function Start-FileSystemWatcher {
[cmdletbinding()]
Param (
[string]$Path,
[parameter()]
[ValidateSet('Changed','Created','Deleted','Renamed')]
[string[]]$EventName,
[string]$Filter,
[parameter()]
[System.IO.NotifyFilters]$NotifyFilter,
[switch]$Recurse,
[scriptblock]$Action
)

$FileSystemWatcher = New-Object System.IO.FileSystemWatcher

If (-NOT $PSBoundParameters.ContainsKey('Path')){
$Path = $PWD
}

$FileSystemWatcher.Path = $Path

If ($PSBoundParameters.ContainsKey('Filter')) {
$FileSystemWatcher.Filter = $Filter
}

If ($PSBoundParameters.ContainsKey('NotifyFilter')) {
$FileSystemWatcher.NotifyFilter = $NotifyFilter
}

If ($PSBoundParameters.ContainsKey('Recurse')) {
$FileSystemWatcher.IncludeSubdirectories = $True
}

If (-NOT $PSBoundParameters.ContainsKey('EventName')){
$EventName = 'Changed','Created','Deleted','Renamed'
}

If (-NOT $PSBoundParameters.ContainsKey('Action')){
$Action = {

Switch ($Event.SourceEventArgs.ChangeType) {
'Renamed' {
$Object = "{0} was {1} to {2} at {3}" -f $Event.SourceArgs[-1].OldFullPath,
$Event.SourceEventArgs.ChangeType,
$Event.SourceArgs[-1].FullPath,
$Event.TimeGenerated
}
Default {
$Object = "{0} was {1} at {2}" -f $Event.SourceEventArgs.FullPath,
$Event.SourceEventArgs.ChangeType,
$Event.TimeGenerated
}
}

$WriteHostParams = @{
ForegroundColor = 'Green'
BackgroundColor = 'Black'
Object = $Object
}

Write-Host @WriteHostParams
}
}

$ObjectEventParams = @{
InputObject = $FileSystemWatcher
Action = $Action
}

ForEach ($Item in $EventName) {
$ObjectEventParams.EventName = $Item
$ObjectEventParams.SourceIdentifier = "File.$($Item)"
Write-Verbose "Starting watcher for Event: $($Item)"
$Null = Register-ObjectEvent @ObjectEventParams
}
}

Start-FileSystemWatcher -Path 'C:\Users\me\Downloads\Input_Test' -EventName Changed -Filter '*.js'

它似乎完全按照你的意愿行事......

关于php - 使用 Powershell 的文件观察器并不总是触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36177875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com