2017-01-31 62 views
1

我试图使用Powershell将一大堆文件从一个目录复制到另一个目录中。使用通配符复制文件夹

我用Get-ChildItem C:\Users\Tom\Google Drive\My Files\*\Assessment 1\*来确定这是我想复制的路径,我知道Copy-Item,但我想在复制时保留部分路径名。

例子:

如果我从C:\Users\Tom\Google Drive\My Files\Cool Stuff\Assessment 1\* 复制我要的文件去一个是创建一个名为C:\Users\Tom\Archive\Cool Stuff\Assessment 1

而如果我从C:\Users\Tom\Google Drive\My Files\New Stuff\Assessment 1\*

复制我要的文件去文件夹创建的文件夹名为C:\Users\Tom\Archive\New Stuff\Assessment 1

回答

2

您可以使用Get-ChildItem cmdlet递归Ÿ找到你的基地目录中的所有文件夹Assessment 1,然后使用-replace使用Copy-Item cmdlet的最后复制的项目中删除基本路径:

$baseDir = 'C:\Users\Tom\Google Drive\My Files\' 
$destination = 'C:\Users\Tom\Archive\' 

Get-ChildItem $baseDir -directory -Filter 'Assessment 1' -Recurse | ForEach-Object { 
    $newPath = Join-Path $destination ($_.FullName -replace [regex]::Escape($baseDir)) 
    Copy-Item $_.FullName $newPath -Force -Recurse 

} 
+0

非常感谢您! –