Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell将某些特定文件和某些子文件夹复制到目标_Powershell_Batch Processing_Copy Item - Fatal编程技术网

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

Powershell将某些特定文件和某些子文件夹复制到目标,powershell,batch-processing,copy-item,Powershell,Batch Processing,Copy Item,我有一个文件夹源文件。它有以下文件: image_00000.jpg ... ... image_08000.jpg 以及一些结尾附加了负片的子文件夹: 我的目标是复制特定的文件,比如从除image\u 00000.jpg之外的所有文件复制到image\u 00250.jpgset。此外,我还需要复制一些子文件夹,这些子文件夹末尾附加了负片。 我写了以下脚本: cls $source = "All_flowers_images_negatives" $destination = "Passi

我有一个文件夹源文件。它有以下文件:

image_00000.jpg
...
...
image_08000.jpg
以及一些结尾附加了负片的子文件夹:

我的目标是复制特定的文件,比如从除image\u 00000.jpg之外的所有文件复制到image\u 00250.jpg
set。此外,我还需要复制一些子文件夹,这些子文件夹末尾附加了负片。

我写了以下脚本:

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)
    }
}

为什么?我终于明白了。因为我对Powershell完全不熟悉,所以根本无法编写任何正确的代码。我在“复制”项中查看了一些细节,发现由于我在复制文件时使用了相同的命令,所以它可以工作,但在子文件夹中需要稍作更改。

以下是正确的解决方案:

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)
    }
}

执行路径替换工作,无需修剪,因为不涉及任何文件

请尝试
复制项-递归…
很抱歉,我编写了错误的代码。请参见代码中的“我的编辑”。现在用同样的问题回答问题persisting@Kayasax:我试过-递归。成功了。我的目标已经实现了,但我也犯了错误!(尽管我现在不在乎)。请参阅编辑尝试打印
$i.FullName.Replace($source,$destination)。修剪($i.Name)
以检查获得的完整路径
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)
    }
}
$i.FullName.Replace($source,$destination)