繁体   English   中英

Powershell将某些特定文件和某些子文件夹复制到目标位置

[英]Powershell to copy some specific files and some subfolders to destination

我有一个文件夹 它具有以下文件:

image_00000.jpg
...
...
image_08000.jpg

并且在子文件夹的末尾附加了负号

我的目标是将特定文件(例如image_00000.jpg以外的所有内容) 复制到image_00250.jpg集中。 另外,我还需要复制一些子文件夹,这些子文件夹的末尾附加负号

我写了以下脚本:

cls

$source = "All_flowers_images_negatives"
$destination = "Passion_Flower_Negative_Images_temp" 
<#
foreach ($i in Get-ChildItem -Path $source -Recurse)
{
    if ($i.Name -match "image_(?!(000\d\d|001\d\d|002[0-4]\d|0025[0-1]))\d{5}\.jpg")
    {
        Copy-Item -Path $i.FullName -Destination $i.FullName.Replace($source,$destination).Trim($i.Name)
    }
}
#>
foreach ($i in Get-ChildItem -Path $source -Recurse)
{
    if ($i.Name -match "Negatives$")
    {
        Copy-Item -Path $i.FullName -Destination $i.FullName.Replace($source,$destination).Trim($i.Name)
    }
}

选择性文件复制有效(这就是为什么我将其注释掉了),但是第二个命令仅复制文件夹结构,而不复制那些子文件夹内的任何文件。 我应该做些什么改变?

EDIT1:该代码已被编辑,问题仍然存在。
EDIT2:它与copy-item -recurse ,虽然我的目标已经实现,但是我仍然在

Copy-Item : The given path's format is not supported.
At F:\_102flowers-500X500\copy_positives_Passion_Flower.ps1:18 char:18
+         Copy-Item <<<<  -Recurse -Path $i.FullName -Destination $i.FullName.Replace($source,$destination).Trim($i.Name)
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], NotSupportedException
    + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand

为什么?

终于我明白了。 由于我完全不熟悉Powershell,因此根本无法编写任何适当的代码。 我仔细检查了Copy-Item的细节,发现,由于我使用相同的命令来复制文件,因此可以工作,但对于子文件夹,则需要稍作更改。

这是正确的解决方案:

cls

$source = "All_flowers_images_negatives"
$destination = "Passion_Flower_Negative_Images_temp" 
<#
foreach ($i in Get-ChildItem -Path $source -Recurse)
{
    if ($i.Name -match "image_(?!(000\d\d|001\d\d|002[0-4]\d|0025[0-1]))\d{5}\.jpg")
    {
        Copy-Item -Path $i.FullName -Destination $i.FullName.Replace($source,$destination).Trim($i.Name)
    }
}
#>
foreach ($i in Get-ChildItem -Path $source -Recurse)
{
    if ($i.Name -match "Negatives$")
    {
        Copy-Item -Recurse -Path $i.FullName -Destination $i.FullName.Replace($source,$destination)
    }
}

问题是,需要使用Trim($i.Name)来修剪文件夹完整路径中的文件名(使之有意义),但如果要复制子目录,编写,

$i.FullName.Replace($source,$destination)

做替换路径的工作,不需要修剪,因为不涉及文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM