Get new/changed files from WPD-device, e.g. Android. Enhanced version of Christophe Geers’ PortableDevices

I stumbled upon this nice project by Christophe Geers.

And edited and enhanced it to a tool that works a bit like an advanced REPLACE /UPDATE  to a tree on the PC based on the name of the device and the drives it exposes.

It is an efficient way to get changed & new files from a device to a PC.

Update: Now supports Exclude also.

It works with a WPD connected device, e.g. an Android device USB-connected in MTP or PTP mode.

Here an example of some of the tree generated

Most backup tools does NOT allow you to do an incremental backup, but they DO allow you to deselect e.g. images. So use this tool to do the incremental backup of the huge WPD-exposed stuff like the images/videos/music, and let your backup tool do backups of all the rest – and in more finite time than a total backup of everything every time – thus making it a bliss to take backups much more often….

The above picture is from my BB Priv on 6.0.1

It also works on my Ancient Xperia Pro on ICS 4.0.1 (!) when connecting in MTP mode (*) , and a Samsung S8- on Oreo 8.0.0. Also tested working on the same S8- on 9.0 Pie, and the FxTec Pro1 on 9.0 Pie.

The actions on existing files can be handled by command line flags, by default, files on pc are overwritten if they are not the same size and date.

IF the device is rooted (my Priv isn’t) it also tries to modify time-stamps on images on the device to the DateTaken stamp.
This should help if you have copied files from a previous device (or from an old copy if the device has been reset)

(If you copy files from a PC to a WPD device with the explorer the files get the stamp of the time they are copied NOT the stamp of the source files, making a total mess of e.g. image-history, This is a known issue using e.g. Android !!!)

It is NOT a complete backup that is provided, since not all content is exposed through the WPD interface. E.g. your SMS and MMS messages and most apps data are NOT included.

It is the same content you can find in a windows EXPLORER.

But as you can not mount this as a drive letter, you can not make a simple REPLACE /U, thus the need for something like this open source tool…., unless you want to copy everything, and manually handle conflicts for different&equal files…

It should be said that it is quite a task to find code that uses WPD that is not just displaying principles, but actually works(!). So again: Kudos to Christophe Geers his code only has a few very minor issues that needed to be corrected (it might well be that it would work flawlessly on 2011 devices).

I have changed the principal behaviour from collecting the full tree initially, to handling each item as encountered through a call back – the original behaviour is stll in the code through an alternative call back.

(2017-02-02) I found a strange bug on WPD sometimes reporting wrong file-sizes, and updated the program to handle/recognise this. Unfortunately there are no good workaround, so currently it will do a binary compare of files with same date, but reported different sizes. And if the size is the only difference, it is output in a new output group (moving up 5&6 to 6&7 sorry)

 

Due to the CallBack it would thus be quite easy to change this tool to something that does something else with the information collected.

 

The C# source and project-files can be found here (Newest: v12, 2019-01-27)

PortableDevicesIncrementalCopy_V13

and executable, including Interop dll’s

PortableDevicesIncrementalCopy_EXE

Please note that due to a(nother) bug in the Interop DLL generated, it MUST be compiled as x86 (Compiled in x64 or AnyCPU it works, but throws an exception on program-termination)

 

Please fell free to add any suggestions for additional or modified functionality in the comments below.

(No guarantee that it will be implemented though, if you want to fiddle with the code and need some info, also just ask)

 

USAGE

Call the program with ? for help on possible parameters.

It currently defaults to copy to “c:\MTP_DEVICES\” (overridable by command line parameter) under which a directory for each connected device is created..

PortableDevicesIncrementalCopy  see https://eskerahn.dk/?p=1668
PortableDevicesIncrementalCopy [DestFolderRoot] [NoLog] [IncludeFolderOutput]
       [AlwaysCopy|IgnoreSize|IgnoreStamp|OverruleStamp] [ResLevelMax=n] [/?]
       [Excl {\ | }]
   DestFolderRoot, Default C:\MTP_Devices\
   NoLog, Default a log file is created apart from the console output
   IncludeFolderOutput : if set include lines with folder names
   ResLevelMax= : defines the amount of output. Default=5
     0 or higher: include (Copied)
     1 or higher: include (Skip exists)
     2 or higher: include (Skip exists sam siz)
     3 or higher: include (Cmp: bin equ)
     4 or higher: include (Cmp: bin equ older on device)
     5 or higher: include (Wrong size reported by WPD, bin equ)
     6 or higher: include (Skip exists sam siz & dat)
   PauseAtEnd : Yes you guessed it....
 The next flags are mutually exclusive, checked in this order (default NONE)
   AlwaysCopy : Always copy
   IgnoreSize : Skip if file exist, ignore size
   IgnoreStamp : Skip if same size, ignore stamp
   OverruleStamp : Binary compare even if size and stamps matches
 Excl the rest is seen as exclude masks list, e.g. *cache*\ *.tmp

 

NOTE: Known bug in .NET
Multiple connected devices does not work due to .NET bug:
Unfortunately .NET incorrectly requires WPD GetDevices to be called with a STRING and not an ARRAY of strings.
There is an old 2006(?) workaround described several places including here , I can not get it to work (any more) though ;(

So only one device can be handled at a time.

Update 2018-03-06. I FINALLY got the trick described to work. What I precisely did is described in FULL detail in a comment in PortableDeviceCollection.cs
Note that you may need to manually replace the wrong version of  Interop.PortableDeviceApiLib.dll with the corrected one multiple times, also in bin and obj…

Note that I explicitly ignores portable devices that got a drive-letter attached, as there are SO many other ways to keep them synced. e.g. RoboCopy. So if you got a phone and a standard usb-key attached, only the phone is accessed.

 

Update 2019-01-27. Added the option to EXCLUDE files or folders, especially to exclude CACHE folders. Note the it only work on a general level, so you can not (currently) skip a specific mask one place and include it in another. That is –Excl Myfolder\*cache* would not work as you would expect, but would skip nothing….

 

(*) Note on ICS. I experienced a WPD-lock-up after copying a lot of items. Restart of the program got stuck on the exact same item inside some deep cache folder, and only continued after a long time with an exception, hanging on the next files too… BUT it was the WPD also outside the program that got sick (could not access that file through windows explorer either). After unplugging the cable, restarting the Xperia Pro and reinserting the cable, a rerun fetched the rest… (a cable unplug and re-plug was not enough)