簡體   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