0

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

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

$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文件目录下。

谢谢

4

1 回答 1

0

您想要扩展这些文本文件的 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}}
于 2014-07-31T21:57:27.477 回答