我正在尝试使用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
答案 0 :(得分: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