简体   繁体   English

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

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

I have a folder source . 我有一个文件夹 It has the following files: 它具有以下文件:

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

And some subfolders which have Negatives appended at the end: 并且在子文件夹的末尾附加了负号

My goal is to copy specific files say from, everything except image_00000.jpg to image_00250.jpg set. 我的目标是将特定文件(例如image_00000.jpg以外的所有内容) 复制到image_00250.jpg集中。 In addition I also need to copy some subfolders having which have Negatives appended at the end. 另外,我还需要复制一些子文件夹,这些子文件夹的末尾附加负号

I wrote the following script: 我写了以下脚本:

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

Selective file copy works(thats why I have commented it out) but the second command copies only the folder structure not any files inside those subfolders. 选择性文件复制有效(这就是为什么我将其注释掉了),但是第二个命令仅复制文件夹结构,而不复制那些子文件夹内的任何文件。 What changes should I make? 我应该做些什么改变?

EDIT1: The code has been edited with the problem still persisting. EDIT1:该代码已被编辑,问题仍然存在。
EDIT2: It works with copy-item -recurse and my objective has been achieved but still I am getting, 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

Why? 为什么?

Finally I figured it out. 终于我明白了。 Because of my complete unfamiliarity with Powershell, I couldn't write any proper code at all. 由于我完全不熟悉Powershell,因此根本无法编写任何适当的代码。 I went to little details scrutiny in the Copy-Item and found that, since I had used the same command for copying files it worked but for subfolder it needed a slight change. 我仔细检查了Copy-Item的细节,发现,由于我使用相同的命令来复制文件,因此可以工作,但对于子文件夹,则需要稍作更改。

Here is the correct solution: 这是正确的解决方案:

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

The thing is, Trim($i.Name) is needed for trimming the name of the files from folder complete path-Makes sense but in case of copying the sub-directories, writing, 问题是,需要使用Trim($i.Name)来修剪文件夹完整路径中的文件名(使之有意义),但如果要复制子目录,编写,

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

does the job for replacement of path and no trimming is needed because there is no file involved 做替换路径的工作,不需要修剪,因为不涉及文件

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

相关问题 PowerShell将文件复制到目标的子文件夹,同时排除目标中的某些文件夹 - PowerShell to copy files to destination's subfolders while excluding certain folders in the destination 使用 PowerShell 的没有一些子文件夹和文件的存档文件夹 - Archive folder without some subfolders and files using PowerShell 使用 Powershell 脚本从子文件夹复制特定类型的文件 - Copy specific type files from subfolders using a Powershell script Powershell:将所选文件复制到子文件夹中 - Powershell: Copy selected files in subfolders 动态地将文件从文件夹和子文件夹复制到目标位置 - Dynamcially copy files from folders and subfolders to destination 将所有文件从所有子文件夹移动到新目的地,并按Powershell中每个组的特定文件数量进行处理 - Move all files from all subfolders to new destination with grupping them by specific number of files per group in powershell PowerShell-将某些子文件夹批量移动到新服务器 - PowerShell - bulk move of some subfolders to new server 从某些子文件夹中导入 PowerShell 模块 - Import PowerShell module from within some subfolders 用于特定文件夹和副本的Powershell搜索子文件夹 - Powershell search subfolders for specific folder and copy Powershell 将文件复制到新目标的脚本 - Powershell Script to Copy Files to new Destination
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM