Copy File On Creation (once complete)

后端 未结 1 1989
轮回少年
轮回少年 2021-01-28 17:27

I have the below code to detect the creation of a new file and grab a copy of it.

$folder = \'\\\\server\\share\\monitor_me\\\'
$filter = \'*.*\'  
$copyToFolder         


        
相关标签:
1条回答
  • 2021-01-28 17:48

    Resolved by changing the monitored event to CHANGED. It seems 2 change events occur after the initial create. For simplicity I just monitor for any change event (thus ensuring I'll always have the latest copy) and include the -Force parameter to allow the destination file to be overwritten.

    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
    }
    

    NB: I'm not 100% sure whether this will now capture all new files; as potentially there's some way to create a file without also triggering a change event (i.e. I've not found any documentation to say this scenario doesn't exist); but seems good enough for my current purposes.

    0 讨论(0)
提交回复
热议问题