【问题标题】:Copy directories when their name matches -Like txt files当名称匹配时复制目录 - 像 txt 文件
【发布时间】:2014-09-24 00:26:08
【问题描述】:

我正在尝试复制一堆目录。我有名称相似的文本文件,我需要将目录与这些文本文件匹配以确定要复制的目录。

这就是我所做的,我不知道如何解决它。我在兜圈子。

$destination = "..\..\$args\Images\"
$txtfiles = Get-ChildItem $destination -Include *.txt
$source = "..\..\..\Images\" | ?{ $_.PSIsContainer } | Where-Object { $_.Name -Like "*$txtfiles*" } | Copy-Item $destination

txt 文件示例:1e03655b-0aac-48b2-82f3-75942084af7a.txt

和文件夹:name.1e03655b-0aac-48b2-82f3-75942084af7a

所以我需要找到与txt文件匹配的文件夹,并将文件夹复制到txt文件目录下。

谢谢

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0 powershell-ise powershell-1.0


    【解决方案1】:

    您想要扩展这些文本文件的 BaseName 属性(以切断 .txt 部分)并拥有一个字符串数组,例如:

    1e03655b-0aac-48b2-82f3-75942084af7a
    1e03655b-0aac-48b2-82f3-75942084af7b
    1e03655b-0aac-48b2-82f3-75942084af8g
    1e03655b-0aac-48b2-82f3-75942084afba
    

    然后您的$source = 行需要提取目录列表,而不仅仅是通过管道传递字符串,然后我建议将目录名称与正则表达式匹配,并检查该匹配是否为-in $txtfiles .

    $destination = "..\..\$args\Images\"
    $txtfiles = Get-ChildItem $destination -Filter *.txt | Select -ExpandProperty BaseName
    $source = "..\..\..\Images\"
    GCI $source -Directory | ?{ $_.Name -match "(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})$" } | %{If($Matches[1] -in $txtfiles){Copy-Item $_.FullName $destination}}
    

    【讨论】:

    • 感谢您的快速回复。这对我不起作用。没有错误。
    • 那是因为文件系统提供者像一盒石头一样愚蠢。我将 -Include 更改为 -Filter,它现在可以工作了。
    • 我似乎仍然无法让它工作。这次出现错误 - Copy-Item: A positional parameter cannot be found that accepts argument '..\..\passed_directory\Images\'.
    • 对不起,我重新输入了它而不是复制/粘贴,显然是胖手指。我在'Copy-Item - $_.FullName $destination'命令中有一个-,在搞砸它的源路径之前。删除它后,它就可以工作了。真的很抱歉。
    • 好的,它会复制目录,但不会复制内容。
    猜你喜欢
    • 2021-10-04
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 2014-05-20
    相关资源
    最近更新 更多