Skip to content Skip to sidebar Skip to footer

Script That Will Transfer Photos From Phone Camera Using Adb

Story I take photos and record videos with my phone camera and keep all of them on my internal storage/sdcard. I periodically back them up on my PC, so I keep these camera photos o

Solution 1:

Windows batch script

Here's a .bat script that can be run by Windows Command Prompt or Windows PowerShell. No Git Bash required.

:: Start deamon of the device attached
adb devices

:: Pull camera files starting from dateset srcFolder=/storage/06CB-C9CE/DCIM/Camera
set dstFolder=%USERPROFILE%\Desktop\CameraPhotos
set lsFile=%USERPROFILE%\Desktop\CameraPhotos\camera-ls.txt
set dateRegex=2019061[5-9]_.*

mkdir %dstFolder%
adb shell ls %srcFolder% | adb shell grep %dateRegex% > %lsFile%
for /F "tokens=*" %%A in (%lsFile%) do adb pull %srcFolder%/%%A %dstFolder%
del %lsFile%
echo Done pulling files to %dstFolder%
  • Just edit the srcFolder to point to your phone camera folder,
  • plug a pattern into the dateRegex for matching the date interval and
  • save it as a file with .bat extension, i.e: adb-pull-camera-photos.bat.
  • Double-click the file and it will pull filtered photos into CameraPhotos folder on Desktop.

Keep in mind that you still need have adb for Windows on your PC.

Solution 2:

The problem was with Windows line delimiters.

Easy fix

Just add the IFS=$'\r\n' above the loop so that the read command knows the actual line delimiter.

IFS=$'\r\n'whileread filename; doif [ -z "$filename" ]; thencontinue; fi
  adb pull "$srcFolder/$filename""$dstFolder"done < "$lsFile"

Explanation

I tried plugging the whole while-loop into the console and it failed with the same error:

$ bash adb-pull-camera-photos.shListof devices attached
9889db343047534336      device

tr: warning: an unescaped backslash at end ofstring is not portable
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpgDone pulling files to C:/Users/User/Desktop/CameraPhotos

This time I started investigating why the output was broken. I remembered that windows uses \r\n as newline, which means Carriage Return + Line Feed, (CR+LF), so some text must have been overwritten.

It was because of broken values stored inside the $filename variable.

This is the loop from the script:

whileread filename; doif [ -z "$filename" ]; thencontinue; fi
  adb pull "$srcFolder/$filename""$dstFolder"done < "$lsFile"

Since each iteration of the while-loop reads a line from $lsFile in the following form:

exampleFilename.jpg\r\n

It misinterprets the newline symbols as part of the file name, so adb pull tries to read files with these whitespaces in their names, but fails and it additionally writes a broken output.

Solution 3:

Adb Photo Sync

This might not be the answer but might be useful for others looking for android photo/files backup solution.

I use this script on my Windows with git bash. This can be easily used for Linux. A common issue with a long backup process is that it might get interrupted and you might have to restart the entire copy process from start.

This script saves you from this trouble. You can restart the script or interrupt in between but it will resume copy operation from the point it left.

Just change the rfolder => android folder, lfolder => local folder

#!/bin/sh

rfolder=sdcard/DCIM/Camera
lfolder=/f/mylocal/s8-backup/Camera

adb shell ls"$rfolder" > android.files

ls -1 "$lfolder" > local.files

rm -f update.files
touch update.files

while IFS=  read -r q; do# Remove non-printable characters (are not visible on console)
  l=$(echo${q} | sed 's/[^[:print:]]//')
  # Populate files to updateif ! grep -q "$l" local.files; thenecho"$l" >> update.files
  fidone < android.files

script_dir=$(pwd)
cd$lfolderwhile IFS=  read -r q; do# Remove non-printable characters (are not visible on console)
  l=$(echo${q} | sed 's/[^[:print:]]//')
  echo"Get file: $l"
  adb pull "$rfolder/$l"done < "${script_dir}"/update.files

Post a Comment for "Script That Will Transfer Photos From Phone Camera Using Adb"