gpt4 book ai didi

powershell - FileSystemWatcher检测何时将文件 move 到文件夹

转载 作者:行者123 更新时间:2023-12-03 01:11:31 27 4
gpt4 key购买 nike

我在监视文件夹的脚本时遇到了麻烦。
FileSystemWatcher似乎仅检测何时将文件复制到文件夹,而不是何时将其从同一驱动器移动到文件夹。
有没有办法检测到这一点?

$desFolder   = "H:\Media"
$ExFolder = "H:\Monitor"

$Filter = '*.*'
$fsw = New-Object IO.FileSystemWatcher $ExFolder, $Filter
$fsw.IncludeSubdirectories = $true

$fswOnChange = Register-ObjectEvent -InputObject $fsw -EventName Changed -SourceIdentifier FileUpdated -Action {
$File = Get-Item $EventArgs.FullPath
if($File.Name -imatch '\.(?:mp4|mkv)$' -and $file.Name -match '.*?S\d+E+'){
Start-Sleep -s 1
if(Test-FileReady $File.FullName){
Move-Files $File
}
}
}
function global:Test-FileReady {
Param([parameter(Mandatory=$true)][string]$Path)
if (Test-Path -LiteralPath $Path) {
trap {
return $false
}
$stream = New-Object system.IO.StreamReader $Path
if ($stream) {
$stream.Close()
return $true
}
}
}
function global:Move-Files{
Param([parameter(Mandatory=$true)][System.IO.FileInfo]$File)
Write-Host $File.Name
}

最佳答案

尝试同时使用RenamedCreated事件。
在这之间,IO.FileSystemWatcher docs说:

The component will not watch the specified directory until the Path is set, and EnableRaisingEvents is true

$fsw.EnableRaisingEvents = $true

关于powershell - FileSystemWatcher检测何时将文件 move 到文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38877225/

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