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:
还有一些子文件夹在最后添加了Negative:
My goal is to copy specific files say from, everything except image_00000.jpg to image_00250.jpg set. In addition I also need to copy some subfolders having which have Negatives appended at the end.
我的目标是复制特定文件,除了image_00000.jpg之外的所有内容都设置为image_00250.jpg。另外,我还需要复制一些在末尾附加了Negative的子文件夹。
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.
EDIT2: It works with copy-item -recurse
and my objective has been achieved but still I am getting,
编辑1:代码已编辑,问题仍然存在。 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?
1 个解决方案
#1
Finally I figured it out. Because of my complete unfamiliarity with Powershell, I couldn't write any proper code at all. 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.
最后我明白了。由于我完全不熟悉Powershell,我根本无法编写任何正确的代码。我在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,
问题是,需要修剪($ 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
是否需要更换路径并且不需要修剪,因为没有涉及文件
#1
Finally I figured it out. Because of my complete unfamiliarity with Powershell, I couldn't write any proper code at all. 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.
最后我明白了。由于我完全不熟悉Powershell,我根本无法编写任何正确的代码。我在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,
问题是,需要修剪($ 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
是否需要更换路径并且不需要修剪,因为没有涉及文件