简体   繁体   English

根据其他文件的存在复制文件(Windows)

[英]Copy files based on existence of other files (Windows)

I have a folder of images in jpg format called "finalpics" and also another folder ("sourcepics") which has several subfolders containing RAW files in various formats. 我有一个名为“ finalpics”的jpg格式的图像文件夹,还有另一个文件夹(“ sourcepics”),其中包含几个子文件夹,这些子文件夹包含各种格式的RAW文件。

I need a script (batch file?) that will copy all the files from "sourcepics" and its subfolders to another folder ("sourcefinal") only if that file exists in "finalpics". 我需要一个脚本(批处理文件?),仅当该文件存在于“ finalpics”中时,该脚本才能将所有文件从“ sourcepics”及其子文件夹复制到另一个文件夹(“ sourcefinal”)中。

As an example: 举个例子:

"finalpics" contains files called mypic1.jpg, mypic2.jpg, mypic3.jpg. “ finalpics”包含名为mypic1.jpg,mypic2.jpg,mypic3.jpg的文件。

"sourcepics" contains files called mypic1.dng, mypic2.psd, mypic3.cr2, yourpic1.dng, yourpic2.psd, yourpic3.cr2. “ sourcepics”包含名为mypic1.dng,mypic2.psd,mypic3.cr2,yourpic1.dng,yourpic2.psd,yourpic3.cr2的文件。

I'd want the script to copy the 'mypic' files but not the 'yourpic' files to "sourcefinal". 我希望脚本将“ mypic”文件而不是“ yourpic”文件复制到“ sourcefinal”。

There's over a thousand jpgs in "finalpics" but probably 40,000 files in the various subfolders of "sourcepics". “最终图片”中有超过一千个jpg,但“源图片”的各个子文件夹中可能有40,000个文件。

Hope that makes sense. 希望有道理。

Thanks for looking. 感谢您的光临。

I think this PowerShell code will do what you're after; 我认为此PowerShell代码将满足您的需求; it will copy files of the same name (ignoring file extension) from "SourcePics" to "SourceFinal" if they exist in FinalPics: 如果FinalFics中存在相同名称的文件(忽略文件扩展名),它将从“ SourcePics”复制到“ SourceFinal”:

# Define your folder locations:
$SourcePicsFolder = 'C:\SourcePics'
$FinalPicsFolder = 'C:\FinalPics'
$SourceFinalFolder = 'C:\SourceFinal'

# Get existing files into arrays:
$SourcePics = Get-ChildItem -Path $SourcePicsFolder -Recurse
$FinalPics = Get-ChildItem -Path $FinalPicsFolder -Recurse

# Loop all files in the source folder:
foreach($file in $SourcePics)
{
    # Using the basename property (which ignores file extension), if the $FinalPics 
    # array contains a basename equal to the basename of $file, then copy it:
    if($FinalPics.BaseName -contains $file.BaseName)
    {
        Copy-Item -Path $file.FullName -Destination $SourceFinalFolder
    }
}

Note: There is no filtering based on file type (eg it will copy all files). 注意:没有基于文件类型的过滤(例如,它将复制所有文件)。 Also, if your 'SourcePics' folder has two images of the same filename but in different subfolders, and a file of this name also exists in 'FinalPics', then you may get an error about file already existing when it tries to copy for the second time. 另外,如果您的“ SourcePics”文件夹中有两个文件名相同但位于不同子文件夹中的图像,并且“ FinalPics”中也存在此名称的文件,那么当它尝试为该文件复制时,您可能会收到有关该文件已存在的错误。第二次。 To overwrite, use the -Force parameter on the Copy-Item command. 要覆盖,请在Copy-Item命令上使用-Force参数。

I tested the above code with some .dng files in 'SourcePics' and .jpg files in 'FinalPics' and it worked (ignoring the yourpic files). 我用“ SourcePics”中的一些.dng文件和“ FinalPics”中的.jpg文件测试了上面的代码,它起作用了(忽略yourpic文件)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM