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