Powershell 使用通配符复制文件夹
我正在尝试使用Powershell将一大堆文件从我的计算机上的一个目录复制到另一个目录 我使用了Powershell 使用通配符复制文件夹,powershell,Powershell,我正在尝试使用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
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
的文件夹。您可以使用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
}