![](/img/trans.png)
[英]PowerShell to copy files to destination's subfolders while excluding certain folders in the destination
[英]Dynamcially copy files from folders and subfolders to destination
我有一個文件夾
D:\\ Projects \\ PowerShell \\ Common
在“公用”文件夾中,有許多子文件夾和子子文件夾。 我要實現的是尋找一個csv文件並復制到:
E:\\ Projects \\ PowerShell2 \\ Common
** D:\\ Projects \\ PowerShell \\ Common和E:\\ Projects \\ PowerShell2 \\ Common具有相同的目錄結構。
例
D:\\ Projects \\ PowerShell \\ Common \\ Geo \\ ABC.csv
D:\\ Projects \\ PowerShell \\ Common \\ Geo \\ MEA.csv
D:\\ Projects \\ PowerShell \\ Common \\ AREA \\ ASA.csv
復制到
E:\\ Projects \\ PowerShell2 \\ Common \\ Geo \\ ABC.csv
E:\\ Projects \\ PowerShell2 \\ Common \\ Geo \\ MEA.csv
E:\\ Projects \\ PowerShell2 \\ Common \\ AREA \\ ASA.csv
如果我完全按照以下方式指定位置,則可以實現此目的,但是我希望腳本能夠動態處理而不是硬代碼處理。 如果存在相同的文件,只需替換它們即可。
$Src = 'D:\Projects\PowerShell\Common\Geo'
$Dst = 'E:\Projects\PowerShell2\Common\Geo'
$Extension = '*.csv'
Get-ChildItem -Path $Src -Filter $Extension -Recurse |
Where-Object {!$_.PsIsContainer} |
# For each file
ForEach-Object
{
some code
}
試試這個,同時復制並創建目錄(如果不存在)
$source='D:\Projects\PowerShell'
$destination='E:\Projects\PowerShell2'
gci $source -file -Filter "*.csv" -Recurse |
%{$newfile=$_.Directory -replace [regex]::Escape($source), $destination; copy-item -Path $_.FullName -Destination (new-item -type directory -force $newfile) -force -ea 0}
如果要日志復制的文件
$source='D:\Projects\PowerShell'
$destination='E:\Projects\PowerShell2'
$logfile="c:\temp\mylog.log"
gci $source -file -Filter "*.csv" -Recurse |
%{$newfile=$_.Directory -replace [regex]::Escape($source), $destination; copy-item -Path $_.FullName -Destination (new-item -type directory -force $newfile) -force -ea 0 -PassThru |
%{ $_.fullname | out-file $logfile -Append}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.