如何使用 Powershell 获取和复制所有具有相同文件名但不同扩展名的文件?

如何使用 Powershell 获取和复制所有具有相同文件名但不同扩展名的文件?

我一直试图将索尼相机中的 RAW 图像 (.ARW) 过滤到 PC 中,并考虑通过查看所有 .JPG 图像并删除我不喜欢的其他 .JPG 图像来实现。一旦我获得所选的 .JPG 图像,我想使用它们来查找所有与我所选的 .JPG 文件相对应的 RAW 文件。

我以为我已经接近完美了,但是我觉得有些不对劲。

$ext_FindName = "JPG"

$source_Path = "c:\Pictures\SONY\RAWandJPG"
$dest_Path = "c:\temp\chosenRAW"

 Get-ChildItem -Path $source_Path -File -Recurse -ErrorAction SilentlyContinue |Where-Object { $_.extension -eq '.'+$ext_GetName -AND (Test-Path "$($_.BaseName).$ext_FindName") } | Copy-Item -Destination $dest_Path

pause

如何将具有相同名称但不同扩展名的照片/文件复制到另一个文件夹?

答案1

如果文件夹中的 JPG 文件与 RAW 扩展名匹配RAWandJPG,则 RAW 将被复制到该chosenRaw文件夹​​中。这假设具有相同扩展名的文件不超过 2 个(例如 1.jpg、1.raw)

$source_Path = "c:\Pictures\SONY\RAWandJPG"
$dest_Path = "c:\temp\chosenRAW"
$include = "*.jpg","*.raw"

$files = Get-ChildItem $source_Path -Include $include -Recurse -Force -ErrorAction SilentlyContinue | group Basename 

foreach($group in $groups)
{ 
    if ($group.Count -eq 2)
    {
       foreach($file in $group.group)
       {
            if(($file.name).Contains('.raw')) 
            { 
                Copy-Item $file.fullname $dest_Path
            }
        } 
    }
}

答案2

感谢@Narzard 的帮助。我创建了一个类似于您分享的脚本,但不确定我的脚本是否有效,但它也能完成它的工作……

## INPUT DETAILS
$source_Path = Read-Host -Prompt 'Enter source path'
$dest_Path = Read-Host -Prompt 'Enter destination path'

$ext_GetName = Read-Host -Prompt 'Enter extension name of RAW file (default: ARW)'
if(-not($ext_GetName)){
    $ext_GetName = 'ARW'
}
elseif($ext_GetName.Length -ge 4){
    Write-output "Extension name is not compatible, make sure you are typing the input max of 3 characters only."
    $ext_GetName = Read-Host -Prompt 'Enter extension name of RAW file (default: ARW)'
}


$ext_FindName = Read-Host -Prompt 'Enter extension name of compressed pictures (default: JPG)'
if(-not($ext_FindName)){
    $ext_FindName = 'JPG'
}
elseif($ext_FindName.Length -ge 4){
    Write-output "Extension name is not compatible, make sure you are typing the input max of 3 characters only."
    $ext_FindName = Read-Host -Prompt 'Enter extension name of compressed pictures (default: JPG)'
}



## IF YOU WANT TO USE STATIC DETAILS
## -------------------#

## EXTENSION NAMES
#$ext_GetName = "CR2"
#$ext_FindName = "JPG"
## PATH
#$source_Path = "F:\Pictures\FAMILY, LOVE, FRIENDS, TRAVEL PHOTOS\_Photo Archives\2022-11-28 (AYALA LIGHTS)"
#$dest_Path = "c:\temp\test"
## -------------------#

$ext_GetName_incl = "*."+$ext_GetName
$ext_FindName_incl = "*."+$ext_FindName

$RAWfiles = Get-ChildItem -Path $source_Path -Include $ext_GetName_incl -Recurse -Force -ErrorAction SilentlyContinue

$COMPfiles = Get-ChildItem -Path $source_Path -Include $ext_FindName_incl -Recurse -Force -ErrorAction SilentlyContinue

write-host "Looking for "$ext_GetName" files for chosen "$ext_FindName" files in "$source_Path

##FOR TESTING## Get-ChildItem -Path $source_Path -Include $ext_xxxxx_xxx -Recurse -Force -ErrorAction SilentlyContinue

$counter=0
foreach( $RAWfile in $RAWfiles ) 
{
    foreach( $COMPfile in $COMPfiles ) 
    {   
        if(($RAWfile.BaseName -eq $COMPfile.BaseName)) 
        { 
            write-host Successfully copied $RAWfile.name 
            Copy-Item $RAWfile.fullname $dest_Path
            $counter++
        }
    }
}

write-host Successfully copied $counter files to $dest_Path
pause

答案3

我将Get-ChildItem为此使用两条单​​独的线。

第一个接收找到的所有 .JPG BaseName 的数组,第二个使用子句Where-Object获取所有具有 BaseName 的 .ARW 文件,我们可以将该 BaseName 与第一个数组中的一个 BaseName 匹配。

$sourcePath  = "F:\Pictures\FAMILY, LOVE, FRIENDS, TRAVEL PHOTOS\_Photo Archives\2022-11-28 (AYALA LIGHTS)"
$destination = "c:\temp\test"

# make sure the destination path exists before copying/moving files to it
$null = New-Item -Path $destination -ItemType Directory -Force

# get the .JPG (sometimes also .JPEG) files list as array of BaseNames
# if you do not have .JPEG extensions, use the more efficient
# -Filter '*.JPG' instead of -Include '*.jpg', '*.jpeg'
$jpegs = (Get-ChildItem -Path $sourcePath -Include '*.jpg', '*.jpeg' -File -Recurse).BaseName

# next get all the raw files (extension .ARW) that have .jpg counterparts and copy them
Get-ChildItem -Path $sourcePath -Filter '*.arw' -File -Recurse | 
Where-Object { $jpegs -contains $_.BaseName } |
Copy-Item -Destination $destination

相关内容