SOLVED

Getting Camera Autoplay path

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.