[英]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.