Backup Phone Pictures and Videos

The carrier (Verizon, Google Fi etc) that a lot of folks use for their smartphones, automatically backs up their photos and videos. Other phones and carriers do not have this feature.  It is a pretty simple process.  This is one of many links that show how to do it: https://www.verizonwireless.com/support/knowledge-base-218524/.

I am helping a very kind (and smart) gentleman extend a program he wrote which automatically does this. Without going into all the boring technical details, I will just say, when you connect your phone to your PC, it’s a little different than connecting USB sticks etc..  Research MTP aka Media Transport Protocol, the https://docs.microsoft.com/en-us/windows/win32/wmdm/windows-media-device-manager-11-sdk etc. for the boring details.

Anyone use a phone that does not automatically backup your photos and videos? Anyone want the capability to do their own backups of these files? It is really simple to do, but, I am looking at a way to do it automatically, by year, or by year and month. If this is an issue for anyone, or anyone you know, please let me know. One scenario is to find a helpful friend with a PC, then ask to plug your phone into their computer — they would only need to push a button to backup all your photos and videos to a USB stick.

Stay tuned to this page for further developments.

The General Flow of Using This Approach

In order to use the program, you will have to do this:

Connect your device by USB cable to a Windows PC.
(Bluetooth or WiFi connectivity is not under consideration at this time.)

Tap “Charging this device” 

InkedTapToSelectFileTansfer_LI
Tap File Transfer.

SelectFileTransfer

You many need to Tap one of the following:
Just Once
Always

Run the program.
This is PowerShell script at this time (a Microsoft program included with Windows).

Programs from outside sources will ask for permission to make changes.allowchanges

PowerShell scripts ask you about execution policy bypass. This allows this instance of the program to run.  If you do not make other changes, it will prompt you every time for this.  For the most part, this is what we recommend.

PowerShell-Execution-Policy

Answer Y

The Program will prompt you for your device name.
You will see a list of suggestions or a guide to selecting it from Windows File Explorer.

The program will attempt to identify if you have an SD card and an “internal device” and show you a partial list of the files at any location that it thinks have Pictures and Videos t you have taken with your device.

[Future editing note – Note show more examples, Consumer Cellular, Cricket etc:]

This PC\Galaxy J7 Crown\Card\DCIM\Camera\
This PC\Galaxy J7 Crown\Phone\DCIM\Camera

The general template is:
This PC\MTP USB Device\Internal shared storage\DCIM\Camera

You will select the source(s) of the pictures you want to copy.

You will be asked is you want to copy them to to a destination folder by Year, by Year and Month or just to a destination folder.

You will be prompted for the main destination drive.
The default will be : C:\Users\username\Pictures
Any accessible writable drive will be possible, including USB sticks.
(If the number of files to copy > 100 give x time warning, if 500, say, copy to the computer’s hard drive first, then transfer to the USB.

You will have to accept the default or enter a valid destination

If there are any errors, they will written to the screen.  A .txt log file will also be created showing any errors, input and output drives, the number of files copied, not copied etc..


This project was inspired by and is possible thanks to the efforts of thousands of people who have fought for our freedoms.

I cannot convey the importance and impact these heroes have had on our lives.   For your consideration, I offer these two items.


The Four Freedoms by United States President Franklin D. Roosevelt

Freedom of speech
Freedom of worship
Freedom from want
Freedom from fear

See https://en.wikipedia.org/wiki/Four_Freedoms


Richard Stallman, one of the most influential champions and pioneers in free and open source software, described another set of freedoms.  The web and thousands of other programs, that millions use daily, have been developed with these freedoms as inspiration.

    • The freedom to run the program for any purpose.
    • The freedom to study how the program works, and change it to make it do what you wish.
    • The freedom to redistribute and make copies so you can help your neighbor.
    • The freedom to improve the program, and release your improvements (and modified versions in general) to the public, so that the whole community benefits.

See https://en.wikipedia.org/wiki/The_Free_Software_Definition


Current Command Line Version of Program:
https://github.com/WillyMoselhy/Weekend-Projects
Download Copy-MTPCameraByMonth.ps1

Click the Search Button and enter PowerShell
CD to the directory where you downloaded the program
Example Command
.\Copy-PhonePics.ps1 “Moto X4\Internal shared storage\DCIM\Camera” C:\JIC\test

This version of the program expects your pics and videos to be in this format
File names follow this pattern yyyyMMdd_HHmmss e.g. 20200104_231922.jpg

In my case it did not work, as my Android 10 Google Fi Moto X4 stores files with either IMG_ or VID_ appended to the name, example

IMG_20181121_183737281.JPG


Code Snippets, and Links to Examples of Possible Use

Get filename and filetype
$filePath = “C:\PS\Test.Documents\myTestFile.txt”;
$fileName = (Split-Path -Path $filePath -Leaf).Split(“.”)[0];
$extension = (Split-Path -Path $filePath -Leaf).Split(“.”)[1];

get year
^(19|20)[\d]{2,2}$

Get Username
Write-Host ‘Your Username is ‘$env:UserName

Get List of Devices
$DeviceName = Get-PnpDevice -Class WPD -PresentOnly  | Select-Object -Property FriendlyName
Write-Host ‘=============’
Write-Host $DeviceName

Find the DCIM directorys for the DeviceName:
Crawl your Android device attached via usb with PowerShell
https://gist.github.com/cveld/8fa339306f8504095815

Interactive examples
https://mcpmag.com/articles/2016/06/09/display-gui-message-boxes-in-powershell.aspx

https://www.business.com/articles/powershell-interactive-menu/

https://www.robvanderwoude.com/powershellstart.php


This project takes the existing code and makes it interactive
and prompting the user for some confirmations and error checks. Also the existing program
does this
# File names follow this pattern yyyyMMdd_HHmmss e.g. 20200104_231922.jpg
# Files that do not match this pattern are excluded and reported

and that did not work for my phone.
Requirements by user

Step 1:
Connect phone by usb cable to windows computer

Step 2:
tap on phone and change from charging to file transfer

Step 3:
Run Program and allow Execution Policy bypass

This might help determine if you can find the device

Get List of Devices
$DeviceName = Get-PnpDevice -Class WPD -PresentOnly | Select-Object -Property FriendlyName
Write-Host ‘=============’
Write-Host $DeviceName

If more than one device, show the device name names returned from above code snippet with a way to select the device you want

Example
1) Galaxy J7 Crown
2) Moto X4

Enter the number next to the name of your phone
Validate the number enter selected the set input device name
{if can find device error exit}

Step 4:
Find name of phone internal storage and optional SD card
Note: See the link below (Crawl your Android… )for a way to crawl 2 or 3 levels to find the level where the DCIM directory
is. DCIM is a standard use by phone makes and camera makers
Find the DCIM directories for the DeviceName:

Crawl your Android device attached via usb with PowerShell

{if can find DCIM error exit}

set the input file path or paths

Step 5:

Check files under DCIM directories for standard format

{if can find any files error exit}
examples file names

$f1=’20200104_231922.jpg’; // default used in existing ps1 file
$f2=’IMG_20181121_183737281.JPG’; // what I see on my device
$f3=’VID_20181121_183737281.MP4′;

regex to find year month day
([12]\d{3}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01]))

Step 6:
A) If you can find files with a year month day, offer to copy from device to destination
by Year
By Year Month

B) else say, could not find year month day file names, do you want to copy all files to destination

Step 7:
prompt for destination drive. {any accessible drive}
Offer the default as

The default will be : ‘C:\Users\’ concatenated with $env:UserName concatenated with ‘\Pictures’;

Check for destination existence
{if can’t find destination error exit}

Step 8:
The existing program does most of this.

This snippet from that program needs work to incorporate the information gathered above

This needs to be changed and expanded with the information from above

## Copy items from camera by month
# We use the file name to arrange folders
# File names follow this pattern yyyyMMdd_HHmmss e.g. 20200104_231922.jpg
# Files that do not match this pattern are excluded and reported

$FileNameRegex = “^(?\d{4})(?\d{2})(?\d{2})_(?\d{2})(?\d{2})(?\d{2}).*\.(?.+)$” # https://regexr.com/45sdj

$ProgressActivityName = “Copying files from ‘$MTPSourcePath’ to ‘$TargetPath'”

$SkippedFiles = @()
$CopiedFilesCount = 0

foreach ($File in ($CameraItems |Sort-Object -Property Name) ){
#Validate file name matches pattern

Write-Progress -Activity $ProgressActivityName -Status “Working on it” -CurrentOperation “Copying: $($File.Name) – Finished $CopiedFilesCount / $($CameraItems.count)” -PercentComplete (($CopiedFilesCount/$CameraItems.count)*100)
$CopiedFilesCount++

if($File.Name -notmatch $FileNameRegex){
$SkippedFiles += [PSCustomObject]@{
Name = $File.Name
TargetPath = $null
Reason = “Pattern mismatch”
}
Write-Warning “$($File.Name) is skipped because of pattern”
}

The destination needs to be set based on the information gathered in step 6

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.