复制项目几乎可以工作,但缺少明显的东西

时间:2019-06-27 13:10:25

标签: powershell copy-item

以下内容几乎可以使用,但是我缺少明显的东西,在SOURCE文件夹ODS中,有一个名为Archive的子文件夹。但是,当我运行代码时,似乎没有on.ly在目标ODS文件夹中创建另一个ODS文件夹,但是该文件夹中存储的所有csv文件都不会被复制,我错误地认为-Filter将确保仅将csv复制到


## 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

1 个答案:

答案 0 :(得分:1)

如评论中所述,解决方案是将try块替换为:

try{
Get-Childitem -Path $sourceDirectory -File -Filter "*.csv" | Copy-Item -Destination $destinationDirectory -Force -Verbose -ErrorAction Stop
}

希望有帮助! BR