0

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
7
  • I Think you are missing the wildcard. Copy-item -Force -Verbose $sourceDirectory -Filter "*.csv" -Recurse -Destination $destinationDirectory -ErrorAction Stop Hope it Helps! BR Jun 27, 2019 at 13:59
  • thanks but its copying everything including subfolders AND creating another ODS folder within the existing ODS fold eg csv's appearing in here now (I:\Dev\BI\Projects\Powershell\Test Area\Source Data\ODS\ODS\Archive). I just want only the csv files in C:\Users\Simon.Evans\Documents\Source Data\ODS\ to be copied into I:\Dev\BI\Projects\Powershell\Test Area\Source Data\ODS\ and nothinbg else.
    – Simon
    Jun 27, 2019 at 14:04
  • currently you are copying the Folder ODS to another Folder ODS. If you want to only copy the files contained perhaps an solution with get-childitem would be better. There you can define to only take files. If I have enough time I'll try to provide code Jun 27, 2019 at 14:12
  • Get-ChildItem -Path '.\Desktop\localtmp\ping\ling ling' -Recurse -File -Filter "*.txt" | Copy-Item -Destination '.\Desktop\ling ling' Jun 27, 2019 at 14:18
  • This line will copy all files with the ending .txt from the folder ling ling to another folder ling ling Jun 27, 2019 at 14:19

1 Answer 1

1

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.