2

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

MAX
  • 21
  • 1
  • You first need to figure out what's actually going wrong during the big file copy. Launch [procmon](https://learn.microsoft.com/en-us/sysinternals/downloads/procmon), filter on process id of the PowerShell process you're testing from, and observe network + file system activity around the time it _should_ have copied a given file – Mathias R. Jessen Mar 04 '22 at 12:03
  • Be aware that `Copy-Item` is _not_ efficient. The time that `Copy-Item` takes to copy a file does not go up linearly with file size; the factor is higher. – Jeff Zeitlin Mar 04 '22 at 12:13
  • Thanks to MAthias and Jeff. I have monitored via procmon but I haven't read what interferes with copy. However I have tried to execute the script on local pc instead of lan and I have observed a curious behaviour: the files are copied on user desktop despite the path in the script points towards other disk. The portion of code modified with new path: $folder = 'E:\ARXivar_Data' $filter = '*.*' $copyToFolder = 'E:\CopiaAcritica\' – MAX Mar 07 '22 at 06:47

0 Answers0