2012-06-05 160 views
4

我需要能够测试,看看目录是否存在于Android设备的SD卡上,然后推送几个文件到该目录,如果它确实存在。检查目录是否存在使用ADB,并推送文件,如果它

到目前为止,我有这样的:

adb %argument% shell if [ -e /sdcard/ ]; then echo "it does exist"; else echo "it does not exist"; fi; 

但我怎么可以让我的批处理脚本知道该目录存在,以便它可以继续执行文件推到该目录?

回答

3

以下是我在批处理脚本都做:

set cmd="adb shell ls | find /c "theFile" " 
FOR /F %%K IN (' !cmd! ') DO SET TEST=%%K 
if !TEST! GTR 0 (
    echo the file exists 
) else (
    echo the file does not exist 
) 

有可能是适合的文件名的多个文件,所以我选择把它考大于0


要测试精确匹配和使用bash在Linux中(reference):

FILENAME_RESULT=$(adb shell ls/| tr -d '\015'|grep '^fileName$') 

if [ -z "$FILENAME_RESULT" ]; 
then 
     echo "No fileName found." 
else 
     echo "fileName found." 
fi 
1

我想你应该列出目录dir or ls然后使用grep分析出来。如果grep发现目录脚本做某事。

+0

在windows机器上没有grep。并且不想下载外部实用程序来实现此目标。 – prolink007

+0

你应该在windows中阅读find和findstr命令。 – user902691

+0

我已经意识到'find',但我该如何使用find来设置一些标志,告诉我的下一个命令将'push'文件推送到设备上? – prolink007

0

1)只要使用adb外壳LS /文件路径> fileifpresent如果“没有这样的文件或目录”目前

2)用grep在本地,然后NO

Else Directory Present 
0

以下是我会做的检查命令的退出状态

MyFile="Random.txt" 
WorkingPath="/data/local/tmp/RandomFolder" 

IsDir=`adb shell ls $WorkingPath &> /dev/null ; echo "$?"` 

if [ $IsDir == 0 ] ; then 

    echo "Exist! Copying File To Remote Folder" 

    adb push $MyFile $WorkingPath 

else 

    echo "Folder Don't Exist! Creating Folder To Start Copying File" 

    adb shell mkdir $WorkingPath 

    adb push $MyFile $WorkingPath 

fi 
相关问题