脚本将使用adb从手机摄像头传输照片 [英] Script that will transfer photos from phone camera using adb

查看:712
本文介绍了脚本将使用adb从手机摄像头传输照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用手机摄像头拍照并录制视频,并将它们全部保存在内部存储器/ SD卡中。我会定期将它们备份到PC上,因此我会将这些相机照片与PC上的手机存储保持同步。

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 on PC storage in sync with phone storage.

多年来,我一直在备份手机相机照片以以下方式发送到我的PC:

For years, I've been backing up my phone camera photos to my PC in the following way:


  1. 将手机插入PC并允许访问手机数据

  2. 浏览手机存储→DCIM→相机

  3. 等待几分钟,系统将加载所有照片列表

  4. 仅复制几张尚未备份的最新照片

  1. Plug in phone into PC and allow access to phone data
  2. Browse phone storage → DCIM → Camera
  3. Wait several minutes for the system to load a list of ALL photos
  4. Copy only several latest photos which haven't been backed up yet

我认为要等待几分钟才能加载所有照片是不必要的拖动,所以我下载了 adb平台工具。我已将文件夹bin添加到我的 Path 环境变量(即%USERPROFILE%\Tools\adb-platform-tools_r28.0.3 ),这样我就可以无缝使用 adb 而不是每次都写其完整路径。

I figured that waiting several minutes for all photos to load is an unnecessary drag so I downloaded adb platform tools. I've added the folder bin to my Path environment variable (i.e. %USERPROFILE%\Tools\adb-platform-tools_r28.0.3) so that I can seamlessly use adb and not write its full path each time.

我为 Git Bash 对于Windows。如果更改 $ userprofile 变量,它也与Unix兼容。本质上,脚本将两个日期之间的相机照片从手机存储拉到PC

I wrote the following script for Git Bash for Windows. It is also compatible with Unix if you change the $userprofile variable. Essentially, the script pulls camera photos between two dates from phone storage to PC.

# Attach device and start deamon process
adb devices

# Initialize needed variables
userprofile=$(echo "$USERPROFILE" | tr "\\" "/") # Windows adjustments

srcFolder="//storage/06CB-C9CE/DCIM/Camera"    # Remote folder
dstFolder="$userprofile/Desktop/CameraPhotos"  # Local folder
lsFile="$dstFolder/camera-ls.txt"
filenameRegex="2019061[5-9]_.*"  # Date from 20190615 to 20190619

# Create dst folder if it doesn't exist
mkdir -p "$dstFolder"

# 1. List contents from src folder
# 2. Filter out file names matching regex
# 3. Write these file names line by line into a ls file
adb shell ls "$srcFolder" | grep -E "$filenameRegex" > "$lsFile"

# Pull files listed in ls file from src to dst folder
while read filename; do
  if [ -z "$filename" ]; then continue; fi
  adb pull "$srcFolder/$filename" "$dstFolder" # adb: error: ...
done < "$lsFile"

# Clean up
rm "$lsFile"

# Inform the user
echo "Done pulling files to $dstFolder"



问题



当我运行脚本时( bash adb-pull-camera-photos.sh ),一切正常,除了 adb pull 命令循环。 它给出以下错误

The problem

When I run the script (bash adb-pull-camera-photos.sh), everything runs smoothly except for the adb pull command in the while-loop. It gives the following error:

': 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.jpg

我不确定为什么输出中断。有时,当我调整Git Bash窗口的大小时,其中的一些文本变得有些混乱。这是实际的错误文本:

I am not sure why the output is broken. Sometimes when I resize the Git Bash window some of the text goes haywire. This is the actual error text:

adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg': No such file or directory

我确定这些文件存在于手机的指定目录中。 当我在bash中手动执行失败的命令时,它成功并显示以下输出

I am sure that these files exist in the specified directory on the phone. When I manually execute the failing command in bash, it succeeds with the following output:

$ adb pull "//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg" "C:/Users/User/Desktop/CameraPhotos/"
//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg: 1 file pulled. 15.4 MB/s (1854453 bytes in 0.115s)



问题



我不知道脚本出了什么问题。我认为Windows系统可能会引起麻烦,因为我看不到手动输入相同代码会起作用,但在脚本中运行时无法起作用的原因。如何解决此错误?

The question

I can't figure out what's wrong with the script. I thought the Windows system might be causing a commotion, because I don't see the reason why the same code works when entered manually, but doesn't work when run in a script. How do I fix this error?


  • 请注意,我已经在Windows绝对路径的开头使用 // ,因为Git Bash会将 / 解释为其自己的根目录( C:\Program Files\Git )。

  • 我已 c 了所有变量

  • Note that I had to use // in the beginning of an absolute path on Windows because Git Bash would interpret / as its own root directory (C:\Program Files\Git).
  • I've echoed all variables inside the script and got all the correct paths that otherwise work via manual method.
20190618_124656.jpg
20190618_204522.jpg
20190619_225739.jpg



其他问题



Additional questions


  1. 是否可以导航到外部sdcard而无需使用其名称?我必须使用 / storage / 06CB-C9CE / ,因为 / sdcard / 可以导航到内部存储。

  2. 为什么 tr \\ / 给我这个错误: tr:警告:不转义的反斜杠字符串末尾不是可移植的

  1. Is it possible to navigate to external sdcard without using its name? I had to use /storage/06CB-C9CE/ because /sdcard/ navigates to internal storage.
  2. Why does tr "\\" "/" give me this error: tr: warning: an unescaped backslash at end of string is not portable?


推荐答案

问题是带有 Windows行定界符的。

只需添加 IFS = $'\r\n'在循环上方,以便 read 命令知道实际的行定界符。 / p>

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

IFS=$'\r\n'
while read filename; do
  if [ -z "$filename" ]; then continue; fi
  adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"



说明



I尝试将整个 while -循环插入控制台,但失败并出现相同错误:

Explanation

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

$ bash adb-pull-camera-photos.sh
List of devices attached
9889db343047534336      device

tr: warning: an unescaped backslash at end of string 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.jpg
Done pulling files to C:/Users/User/Desktop/CameraPhotos

这次我开始调查为什么输出损坏的原因 。我记得Windows使用 \r\n 作为换行符,这意味着回车+换行符(CR + LF),所以有些文本必须已被覆盖。

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.

这是因为 $ filename 变量中存储的值损坏。

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

这是脚本的循环:

while read filename; do
  if [ -z "$filename" ]; then continue; fi
  adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"

由于 while 循环以以下形式从 $ lsFile 中读取一行:

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

exampleFilename.jpg\r\n

它误解了换行符号作为文件的一部分名称,因此adb pull尝试读取名称中带有这些空格的文件,但是失败,并且它还会写入损坏的输出。

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.

这篇关于脚本将使用adb从手机摄像头传输照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆