复制项目不适用于FileSystemWatcher

编程入门 行业动态 更新时间:2024-10-23 09:30:57
本文介绍了复制项目不适用于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

本文发布于:2023-10-24 14:43:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524207.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不适用于   项目   FileSystemWatcher

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!