复制项目不适用于FileSystemWatcher [英] Copy-Item does not work with FileSystemWatcher

查看:85
本文介绍了复制项目不适用于FileSystemWatcher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在网络共享上创建一个监视文件夹,该监视文件夹只是将大小为300mb-20gb的文件复制到目标文件夹. FileSystemWatcher和预订适用于小文件(即1-3kb).但是,较大的文件不会复制.我确实看到详细流中触发了复制,但是没有文件复制到目标文件夹.

I'm trying to create a watch folder on a network share that simply copies files (300mb-20gb) in size to a destination folder. The FileSystemWatcher and subscription works great with small files (i.e. 1-3kb). However, larger files do not copy. I do see a copy is triggered in the verbose stream, but no file is copied to the destination folder.

 $Folder = "\\10.11.233.91\vol_tx01\delivered_media"
 $Filter = "*.mxf"
 $destination = "C:\Users\Leeds TX 11\Desktop\support\Testy"
 $Watcher = New-Object IO.FileSystemWatcher $Folder, $Filter -Property @{
     NotifyFilter = [IO.NotifyFilters]'Filename, LastAccess'
 }

 $onCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier `
 FileCreated -Action {
     $path = $event.SourceEventArgs.FullPath
     $name = $event.SourceEventArgs.Name
     $ChangeType = $event.SourceEventargs.ChangeType
     $Timestamp = $event.TimeGenerated
     Write-Host "The file '$name' was $ChangeType at $Timestamp"
     Copy-Item $path -Destination $destination -Force -Recurse -Verbose
 }

推荐答案

问题的结合就在眼前.首先,感谢JohnLBevan指出LastWrite应该是要使用的notifyfilter.还发现在这种情况下,复制项不会等待源目录中的文件传输关闭.我通过在while循环中等待文件被锁定来解决此问题:

Combination of issue were at hand. Firstly thank you JohnLBevan for pointing out LastWrite should be the notifyfilter to use. Also found that in this case copy-item does not wait for the file transfer in the source directory to close. I fixed this by putting in a while loop waiting for the file to be locked:

 ##################### DANGER BOX ####################################

    $Folder = "C:\Users\Leeds TX 12\Desktop\Source" #Source dir
    $Filter = "*.mxf" # MXF Filter
    $destination = "C:\Users\Leeds TX 12\Desktop\Destination" # Destination dir



################### Watch for file system events###########################

$Watcher = New-Object IO.FilesystemWatcher $Folder, $Filter -Property @{
NotifyFilter = [IO.NotifyFilters]'LastWrite, Filename'
}

################### Register filesystemwatcher & subscribe to notifyfilters ################# 

$onCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier filecreated -Action {
$path = $event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$ChangeType = $Event.SourceEventargs.ChangeType
$Timestamp = $event.TimeGenerated
write-host "The file '$name' was $ChangeType at $Timestamp" # Debug

################# Wait for file lock collapse #########################################

while($True)
{
Try {
      [IO.File]::OpenWrite($path).Close()
      Break
      }
   Catch { Start-Sleep -Seconds 1}
   }

#################### Copy item #############################

Copy-item $path -Destination $Destination -force -Recurse -Verbose}

这篇关于复制项目不适用于FileSystemWatcher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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