Below almost works but I am missing something obvious, within the SOURCE folder ODS I have a sub-folder called Archive
. However when I run the code it appears to not on.ly create another ODS folder in the destination ODS folder but none of the csv files stores in the folder are copies across, I assumed wrongly that -Filter would ensure only the csv's are copied across.
## The location/filename where the Logs will be stored
$varfullpath = "C:\Users\Simon.Evans\Documents\ReferenceData__logfile.txt"
## The location/filename of the Source to copy from
$sourceDirectory = "C:\Users\Simon.Evans\Documents\Source Data\ODS\"
## The location/filename of the Destination to copy to
$destinationDirectory = "I:\Dev\BI\Projects\Powershell\Test Area\Source Data\ODS\"
## Attempts to copy a file fron Source to Destination and record the event, if the Copy-item fails the script is halted and the error messages are captured in the Log
## Possibly only 1 error is needed and or applicable, so remove as necessary.
try{
Copy-item -Force -Verbose $sourceDirectory -Filter ".csv" -Recurse -Destination $destinationDirectory -ErrorAction Stop
Write-Log -Message "Copy from $sourceDirectory to $destinationDirectory suceeded" -path $varfullpath
}
catch{
$Error[0] | Write-Log -path $varfullpath
Write-log -Message "Copy from $sourceDirectory to $destinationDirectory Failed" -Level Error -path $varfullpath
}
Start-Process notepad $varfullpath ## Opens the file immediately for review
As discuessed in the comments the solution would be to replace the try
block with:
try{
Get-Childitem -Path $sourceDirectory -File -Filter "*.csv" | Copy-Item -Destination $destinationDirectory -Force -Verbose -ErrorAction Stop
}
Hope it helps! BR