I\'d like to check whether a file exists on my Android device, and if not - to push it. What is the syntax for doing so using adb on batch? Something like:
i
Running in a command prompt window if /?
or help if
outputs the help pages for internal command if.
The command processor is not very tolerant on syntax. An else
branch is only possible with using parentheses. The true branch must have (
which must be on same line as command if
separated with a space from last argument of the condition.
The closing )
for true branch can be on same line as opening (
or on a different line.
The keyword else
must be on same line as closing )
of the true branch after a space.
If round brackets are used also for the else branch, opening (
must be on same line as else
separated with a space from the keyword.
The keyword do
is completely wrong here as it is a keyword for command FOR.
Some of the working variants.
Everything on one line with as less spaces as possible (hard to read):
if exist "directory\file" (echo exists) else (adb push "file" "directory")
Everything on one line with more spaces for easier reading:
if exist "directory\file" ( echo exists ) else ( adb push "file" "directory" )
Condition on first line, true branch on second line, else branch without parentheses on third line:
if exist "directory\file" (
echo exists
) else adb push "file" "directory"
Each part of the entire condition on separate lines:
if exist "directory\file" (
echo exists
) else (
adb push "file" "directory"
)
There are more variants possible, but they are all horrible to read.
It is best to use second or fourth variant as those two are the best for reading.
I recommend to use always fourth variant if an else branch is used, too.
.\
at beginning of file name / directory name could be omitted.
And the directory separator on Windows is the backslash character.
NOTE: I don't know if if exist ./sdcard/file.any
or .\sdcard\file.any
works at all. The question does not contain any information about
PATH
and PATHEXT
, etc.So the answer covers only the IF syntax mistake, not how to check for existence of a file on any Android device with any Android version connected with whatever device drivers and data storage accessing protocols are used for accessing the device.
I know this is kinda old but how about something around the lines of:
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
)
I am using this for a very similar scenario which works for me. Thought I'd share.