1

我正在尝试使用 powershell 复制一些名称与过滤器匹配的目录。
首先,我删除了在目标路径中匹配的所有目录,之后我尝试复制源目录和内容,但我遇到了问题,因为只复制了目录和子目录中的文件,而不是目录名称和结构。

$source="F:\origin"
$destination="F:\dest"
$filter="@"

# Remove dirs @ 
Get-ChildItem -Path $destination -Recurse | 
Where-Object { $_.DirectoryName -match $filter } | 
remove-item -Recurse

# Copy dirs and all contents
Get-ChildItem -Path $source -Recurse | 
Where-Object { $_.DirectoryName -match $filter } | 
Copy-Item -Destination $destination

我怎样才能做到这一点 ?
谢谢

编辑

F:\origin\@test\index.php
F:\origin\@test1\index1.php
F:\origin\@test1\sub1\subindex1.php
F:\origin\no_consider\index1.php

预期输出

F:\dest\@test\index.php
F:\dest\@test1\index1.php
F:\dest\@test1\sub1\subindex1.php

4

1 回答 1

2

几个小调整似乎已经成功了。用单引号替换了一些双引号,在一行的末尾添加了一个 Recurse 并将 DirectoryName 更改为第一行的 Name。让我知道这个是否奏效:

$source='c:\origin'
$destination='c:\dest'
$filter="@"

# Remove dirs @ 
Get-ChildItem -Path $destination -Recurse | 
Where-Object { $_.Name -match $filter } | 
remove-item -Recurse


# Copy dirs and all contents
Get-ChildItem -Path $source | 
Where-Object { $_.Name -match $filter } | 
Copy-Item -Destination $destination -Recurse -Force
于 2018-09-06T18:07:14.560 回答