仅将特定的子目录及其内容复制到另一个目录



在下面附加的项目目录结构中,我试图仅将一月的子目录,即Product1_2022-01Product2_2022-01,包括其中包含的文件,复制到档案中的2022-01文件夹中。然而,尽管根据需要复制子目录(和内容(,但一级以上的Product1Product2目录中的二月文件,即文件fileA-02fileB-02,也会被复制。

是否可以只复制子目录及其内容?我已经包含了重现上述场景的代码。我是PowerShell的新手,所以任何帮助或建议都将不胜感激,谢谢。

目录结构

代码:

$source = @("C:ProductTestingTestedProduct1*",
"C:ProductTestingTestedProduct2*"
)
$destination = "C:Archive20222022-01"
Copy-Item -Path $source -Destination $destination -Recurse

您明确告诉它复制Product1*Product2*的全部内容。如果您希望将其缩小到仅子文件夹,请指定整个路径(Product1Product1_2022_01(或仅部分路径(Product1Product*(:

$source = @("C:ProductTestingTestedProduct1Product*",
"C:ProductTestingTestedProduct2Product*"
)
$destination = "C:Archive20222022-01"
Copy-Item -Path $source -Destination $destination -Recurse

最新更新