将文件从文件夹和子文件夹动态复制到目标
Dynamcially copy files from folders and subfolders to destination
我有一个文件夹
D:\Projects\PowerShell\Common
在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\区域\ASA.csv
复制到
E:\Projects\PowerShell2\Common\Geo\ABC.csv
E:\Projects\PowerShell2\Common\Geo\MEA.csv
E:\Projects\PowerShell2\Common\区域\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}}
我有一个文件夹
D:\Projects\PowerShell\Common
在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\区域\ASA.csv
复制到
E:\Projects\PowerShell2\Common\Geo\ABC.csv
E:\Projects\PowerShell2\Common\Geo\MEA.csv
E:\Projects\PowerShell2\Common\区域\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}}