创建文件后立即将其复制到其他路径中

发布于 2025-01-11 19:08:05 字数 1062 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文