创建文件后立即将其复制到其他路径中
我是 PS 的新手,所以我相信你的耐心。
我在 33958395/copy-file-on-creation-once-complete 问题中发现这段代码对我有用,但当它冻结时这些文件大约超过 8-10MB 甚至更多。 完整快速地复制小文件列表。中型文件和大文件不一样。 该脚本找到新文件,但不继续复制,也不关闭作业。 我被迫按 Enter 关闭并取消注册事件。
两个问题: 1 - 基于代码如何解决大文件问题? 2 - 我如何在启动时运行该代码?
这是代码:
$folder = 'G:\TESTcopyfiles\sorgente'
$filter = '*.*'
$copyToFolder = '\\10.0.x.x\e$\destinationfolder\'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories
= $true;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
#protection from previous runs
unregister-event -SourceIdentifier FileCreated -ErrorAction SilentlyContinue
Register-ObjectEvent $fsw Changed -SourceIdentifier FileUpdated -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green
Copy-Item -Path $Event.SourceEventArgs.FullPath -Destination $copyToFolder -Force
}
感谢您的建议。
最大限度
I'm a newbie of PS so I trust in your patience..
I have found in 33958395/copy-file-on-creation-once-complete question this code that works for me but it's freeze when the files are approximatively over 8-10MB and more.
A list o small file are copied fully and quickly. Not the same whit medium and big files.
The script find the new file but not proced with copy and not close the job.
I'm forced to close with Enter and unregister event.
Two questions:
1 - based on code how I can resolve the issue on big files?
2 - how I can run tis code on boot?
This is the code:
$folder = 'G:\TESTcopyfiles\sorgente'
$filter = '*.*'
$copyToFolder = '\\10.0.x.x\e$\destinationfolder\'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories
= $true;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
#protection from previous runs
unregister-event -SourceIdentifier FileCreated -ErrorAction SilentlyContinue
Register-ObjectEvent $fsw Changed -SourceIdentifier FileUpdated -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green
Copy-Item -Path $Event.SourceEventArgs.FullPath -Destination $copyToFolder -Force
}
Thank for suggestions.
MAX
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论