SOLVED

Getting Camera Autoplay path

%3CLINGO-SUB%20id%3D%22lingo-sub-3035950%22%20slang%3D%22en-US%22%3EGetting%20Camera%20Autoplay%20path%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3035950%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EHi%2C%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EI'm%20trying%20to%20create%20a%20script%20to%20import%20pictures%20from%20my%20camera%20directly%20to%20my%20NAS%2C%20but%20having%20a%20problem%20in%20getting%20the%20camera%20path.%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EWhen%20I%20plug%20the%20camera%2C%20I%20see%20on%20Explorer%20as%20%22Canon%20EOS%20R5%22%20and%20tried%20to%20use%20below%20script%20I've%20addapted%20from%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fnosalan%2Fpowershell-mtp-file-transfer%2Fblob%2Fmaster%2Fphone_backup_recursive.ps1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fnosalan%2Fpowershell-mtp-file-transfer%2Fblob%2Fmaster%2Fphone_backup_recursive.ps1%3C%2FA%3E%3CSPAN%3E%26nbsp%3Bbut%20not%20giving%20me%20a%20path%20and%20just%20returning%20System.__ComObject...%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%24o%20%3D%20New-Object%20-ComObject%20Shell.Application%0A%24rootComputerDirectory%20%3D%20%24o.NameSpace(0x11)%0A%24CameraFolder%20%3D%20%24rootComputerDirectory.Items()%20%7C%20Where-Object%20%7B%24_.Name%20-eq%20%22Canon%20EOS%20R5%22%7D%20%7C%20select%20-First%201%0A%0Aif(%24CameraFolder%20-eq%20%24null)%20%0A%7B%0A%20%20%20%20%5BSystem.Windows.MessageBox%5D%3A%3AShow(%22Camera%20not%20foundin%20this%20computer.%20Connect%20your%20camera.%22)%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EIf%20I%20list%20the%20Items%20on%20%24rootComputerDirectory%2C%20I%20see%20my%20camera%20there%2C%20but%20can't%20seem%20to%20be%20able%20to%20find%20a%20path%20I%20could%20use%20later%20to%20copy%20files%20from%20the%20camera%20to%20my%20NAS.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EAny%20ideas%3F%20How%20can%20I%20get%20the%20camera%20path%20to%20use%20with%20Copy-Item%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EThanks%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3035950%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
New Contributor

Hi,

 

I'm trying to create a script to import pictures from my camera directly to my NAS, but having a problem in getting the camera path.

 

When I plug the camera, I see on Explorer as "Canon EOS R5" and tried to use below script I've addapted from https://github.com/nosalan/powershell-mtp-file-transfer/blob/master/phone_backup_recursive.ps1 but not giving me a path and just returning System.__ComObject...

 

 

 

 

$o = New-Object -ComObject Shell.Application
$rootComputerDirectory = $o.NameSpace(0x11)
$CameraFolder = $rootComputerDirectory.Items() | Where-Object {$_.Name -eq "Canon EOS R5"} | select -First 1

if($CameraFolder -eq $null) 
{
    [System.Windows.MessageBox]::Show("Camera not foundin this computer. Connect your camera.")
}

 

 

 

 

If I list the Items on $rootComputerDirectory, I see my camera there, but can't seem to be able to find a path I could use later to copy files from the camera to my NAS.

 

Any ideas? How can I get the camera path to use with Copy-Item?

 

Thanks

2 Replies
best response confirmed by jmike72 (New Contributor)
Solution

@jmike72 

 

Was curious about this myself and found a working script! 

# Windows Powershell Script to move a set of files (based on a filter) from a folder
# on a MTP device (e.g. Android phone) to a folder on a computer, using the Windows Shell.
# By Daiyan Yingyu, 19 March 2018, based on the (non-working) script found here:
#   https://www.pstips.net/access-file-system-against-mtp-connection.html
# as referenced here:
#   https://powershell.org/forums/topic/powershell-mtp-connections/
#
# This Powershell script is provided 'as-is', without any express or implied warranty.
# In no event will the author be held liable for any damages arising from the use of this script.
#
# Again, please note that used 'as-is' this script will MOVE files from you phone:
# the files will be DELETED from the source (the phone) and MOVED to the computer.
#
# If you want to copy files instead, you can replace the MoveHere function call with "CopyHere" instead.
# But once again, the author can take no responsibility for the use, or misuse, of this script.</em>
#
param([string]$phoneName,[string]$sourceFolder,[string]$targetFolder,[string]$filter='(.jpg)|(.mp4)$')
 
function Get-ShellProxy
{
    if( -not $global:ShellProxy)
    {
        $global:ShellProxy = new-object -com Shell.Application
    }
    $global:ShellProxy
}
 
function Get-Phone
{
    param($phoneName)
    $shell = Get-ShellProxy
    # 17 (0x11) = ssfDRIVES from the ShellSpecialFolderConstants (https://msdn.microsoft.com/en-us/library/windows/desktop/bb774096(v=vs.85).aspx)
    # => "My Computer" — the virtual folder that contains everything on the local computer: storage devices, printers, and Control Panel.
    # This folder can also contain mapped network drives.
    $shellItem = $shell.NameSpace(17).self
    $phone = $shellItem.GetFolder.items() | where { $_.name -eq $phoneName }
    return $phone
}
 
function Get-SubFolder
{
    param($parent,[string]$path)
    $pathParts = @( $path.Split([system.io.path]::DirectorySeparatorChar) )
    $current = $parent
    foreach ($pathPart in $pathParts)
    {
        if ($pathPart)
        {
            $current = $current.GetFolder.items() | where { $_.Name -eq $pathPart }
        }
    }
    return $current
}
 
$phoneFolderPath = $sourceFolder
$destinationFolderPath = $targetFolder
# Optionally add additional sub-folders to the destination path, such as one based on date
 
$phone = Get-Phone -phoneName $phoneName
$folder = Get-SubFolder -parent $phone -path $phoneFolderPath
 
$items = @( $folder.GetFolder.items() | where { $_.Name -match $filter } )
if ($items)
{
    $totalItems = $items.count
    if ($totalItems -gt 0)
    {
        # If destination path doesn't exist, create it only if we have some items to move
        if (-not (test-path $destinationFolderPath) )
        {
            $created = new-item -itemtype directory -path $destinationFolderPath
        }
 
        Write-Verbose "Processing Path : $phoneName\$phoneFolderPath"
        Write-Verbose "Moving to : $destinationFolderPath"
 
        $shell = Get-ShellProxy
        $destinationFolder = $shell.Namespace($destinationFolderPath).self
        $count = 0;
        foreach ($item in $items)
        {
            $fileName = $item.Name
 
            ++$count
            $percent = [int](($count * 100) / $totalItems)
            Write-Progress -Activity "Processing Files in $phoneName\$phoneFolderPath" `
                -status "Processing File ${count} / ${totalItems} (${percent}%)" `
                -CurrentOperation $fileName `
                -PercentComplete $percent
 
            # Check the target file doesn't exist:
            $targetFilePath = join-path -path $destinationFolderPath -childPath $fileName
            if (test-path -path $targetFilePath)
            {
                write-error "Destination file exists - file not moved:`n`t$targetFilePath"
            }
            else
            {
                $destinationFolder.GetFolder.MoveHere($item)
                if (test-path -path $targetFilePath)
                {
                    # Optionally do something with the file, such as modify the name (e.g. removed phone-added prefix, etc.)
                }
                else
                {
                    write-error "Failed to move file to destination:`n`t$targetFilePath"
                }
            }
        }
    }
}

 

Save it as a .ps1 file and run it like this: (Adjusted it to my phone name, OnePlus Nord)

 

C:\temp\phone.ps1 -phoneName AC2003 -sourceFolder 'Internal shared storage\DCIM' -targetFolder c:\temp\x -filter '(.jpg)$'

 

It does not support recursive :( 


All credits go to the creator Daiyan!
Link: https://blog.daiyanyingyu.uk/2018/03/20/powershell-mtp/

@Harm_Veenstra thanks, just tested and that worked.