5

I need to be able to test and see if a directory exists on the SD card of an Android device and then push a few files to that directory if it does exist.

So far, I have this:

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

But how can I let my batch script know that the directory exists so that it can continue to push the file to that directory?

Andrew T.
  • 4,637
  • 8
  • 40
  • 60
prolink007
  • 32,590
  • 23
  • 113
  • 179

4 Answers4

4

Here is what I have done in batch script:

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
)

There could be multiple files that fit the fileName, so I chose to have it test greater than 0.


To test for exact match and using bash in 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
Community
  • 1
  • 1
prolink007
  • 32,590
  • 23
  • 113
  • 179
1

I think you should list directory dir or ls and next analyze out using grep. If grep found directory script do something.

BenMorel
  • 31,815
  • 47
  • 169
  • 296
user902691
  • 1,139
  • 4
  • 19
  • 46
0

1) Just use adb shell ls /filepath > fileifpresent

2) grep locally if "No such file or directory" present, then NO

 Else Directory Present
0

Here's how I'll do it checking for the exit status of the command

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
DarkXDroid
  • 303
  • 1
  • 11