I am working on a PowerShell script for PowerShell v3 on a Server 2012 R2 system to recursively search the specified folders in the source path and copy wav files less than 5 minutes old to a destination directory.
In this sample code I would need \\source\path\folder1 and \\source\path\folder2 to be searched for wav files and then have them copied into the root destination directory \\destination\path
I was attempting to setup a scheduled task to run this every 5 minutes thus the reason for trying to use {$_.LastWriteTime -gt (Get-Date).AddMinutes(-5)}
without success. Any other suggestions on how to prevent duplicate copies would be great.
I then would like the script to append to a log file of files copied for future reference with a log filed named the current date.
Any suggestions would be greatly appreciated.
$Dst = '\\destination\path'
$Src = '\\source\path'
$LogFolder = "\\log\path"
$FolderName = "folder1","folder2"
$FileType = '*.wav'
Get-ChildItem -Path $Src -Filter $FolderName -Recurse -Force |
Where-Object {$_.PSIsContainer} |
Where-Object {$_.LastWriteTime -gt (Get-Date).AddMinutes(-5)} |
ForEach-Object {
Copy-Item -Path (Join-Path -Path $_.FullName -ChildPath '\*') -Filter $FileType -Destination $Dst -Force | Out-File $LogFolder $(get-date -f MM-dd-YYYY) + .log
}