【问题标题】:Powershell copy directories and contents where name match with filterPowershell复制名称与过滤器匹配的目录和内容
【发布时间】:2018-09-07 02:07:14
【问题描述】:

我正在尝试使用 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

【问题讨论】:

  • 您能否添加文件夹示例列表和预期输出?
  • 这可能就像添加Recurse 一样简单,所以它会复制目录的内容? Copy-Item -Destination $destination -Recurse
  • @JamesC - -recurse 不工作..

标签: powershell


【解决方案1】:

几个小调整似乎已经成功了。用单引号替换了一些双引号,在一行的末尾添加了一个 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

【讨论】:

  • 完美!这就是我要找的!谢谢
猜你喜欢
  • 2015-09-02
  • 2015-12-03
  • 2021-10-02
  • 2021-06-20
  • 2014-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
相关资源
最近更新 更多