batch scripting - if exist ./sdcard/file.any using adb
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:
if exist ./sdcard/file.any do echo "exists"
else adb push file.any ./sdcard/
android batch-file cmd adb
add a comment |
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:
if exist ./sdcard/file.any do echo "exists"
else adb push file.any ./sdcard/
android batch-file cmd adb
2
Have you tried this:if exist ./sdcard/file.any do (echo "exists") else (adb push file.any ./sdcard/)(one-liner)?
– aschipfl
Nov 30 '15 at 10:26
Results in: "else was unexpected at this time"
– giladpac
Dec 1 '15 at 13:07
You need to put all this in a songle line! try also to put""around./sdcard/file.any; moreover I recommend` as path separator rather than/`...
– aschipfl
Dec 1 '15 at 13:16
add a comment |
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:
if exist ./sdcard/file.any do echo "exists"
else adb push file.any ./sdcard/
android batch-file cmd adb
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:
if exist ./sdcard/file.any do echo "exists"
else adb push file.any ./sdcard/
android batch-file cmd adb
android batch-file cmd adb
edited Nov 30 '15 at 8:38
Vivek
5,793135382
5,793135382
asked Nov 30 '15 at 8:35
giladpac
92
92
2
Have you tried this:if exist ./sdcard/file.any do (echo "exists") else (adb push file.any ./sdcard/)(one-liner)?
– aschipfl
Nov 30 '15 at 10:26
Results in: "else was unexpected at this time"
– giladpac
Dec 1 '15 at 13:07
You need to put all this in a songle line! try also to put""around./sdcard/file.any; moreover I recommend` as path separator rather than/`...
– aschipfl
Dec 1 '15 at 13:16
add a comment |
2
Have you tried this:if exist ./sdcard/file.any do (echo "exists") else (adb push file.any ./sdcard/)(one-liner)?
– aschipfl
Nov 30 '15 at 10:26
Results in: "else was unexpected at this time"
– giladpac
Dec 1 '15 at 13:07
You need to put all this in a songle line! try also to put""around./sdcard/file.any; moreover I recommend` as path separator rather than/`...
– aschipfl
Dec 1 '15 at 13:16
2
2
Have you tried this:
if exist ./sdcard/file.any do (echo "exists") else (adb push file.any ./sdcard/) (one-liner)?– aschipfl
Nov 30 '15 at 10:26
Have you tried this:
if exist ./sdcard/file.any do (echo "exists") else (adb push file.any ./sdcard/) (one-liner)?– aschipfl
Nov 30 '15 at 10:26
Results in: "else was unexpected at this time"
– giladpac
Dec 1 '15 at 13:07
Results in: "else was unexpected at this time"
– giladpac
Dec 1 '15 at 13:07
You need to put all this in a songle line! try also to put
"" around ./sdcard/file.any; moreover I recommend ` as path separator rather than /`...– aschipfl
Dec 1 '15 at 13:16
You need to put all this in a songle line! try also to put
"" around ./sdcard/file.any; moreover I recommend ` as path separator rather than /`...– aschipfl
Dec 1 '15 at 13:16
add a comment |
2 Answers
2
active
oldest
votes
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 by a space from second compare string.
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 "directoryfile" (echo exists) else (adb push "file" "directory")
Everything on one line with more spaces for easier reading:
if exist "directoryfile" ( 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 "directoryfile" (
echo exists
) else adb push "file" "directory"
Each part of the entire condition on separate lines:
if exist "directoryfile" (
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 2 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 .sdcardfile.any works at all. The question does not contain any information about
- Android version,
- the Android device itself,
- how the device is configured regarding to accessing files and directories on its storage medias – as removable storage(s) making it possible to access the files and directories from Windows command line environment or just via Media Transfer Protocol which is not supported from Windows command line environment,
- in which environment this batch file is running, i.e. what is the current directory, what are the values of the environment variables
PATHandPATHEXT, 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.
1. Thanks for the detailed explanation of the if-else syntax. It will be very useful for me.
– giladpac
Dec 6 '15 at 14:55
add a comment |
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.
The idea is good. I don't see any reason for using delayed expansion with the missing linesetlocal EnableDelayedExpansionat top. And I think the command FIND is also not needed as it could be enough as first lineset" FileFound=", as second linefor /F %%I in ('adb shell ls "the file"') do set "FileFound=1"and as third lineif defined FileFound (. But I don't haveadbinstalled and no mobile device connected to withadband so don't know whatadb shell ls "the file"outputs on file existing or file missing to verify the command lines as suggested here.
– Mofi
Apr 24 '18 at 5:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33994369%2fbatch-scripting-if-exist-sdcard-file-any-using-adb%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 by a space from second compare string.
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 "directoryfile" (echo exists) else (adb push "file" "directory")
Everything on one line with more spaces for easier reading:
if exist "directoryfile" ( 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 "directoryfile" (
echo exists
) else adb push "file" "directory"
Each part of the entire condition on separate lines:
if exist "directoryfile" (
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 2 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 .sdcardfile.any works at all. The question does not contain any information about
- Android version,
- the Android device itself,
- how the device is configured regarding to accessing files and directories on its storage medias – as removable storage(s) making it possible to access the files and directories from Windows command line environment or just via Media Transfer Protocol which is not supported from Windows command line environment,
- in which environment this batch file is running, i.e. what is the current directory, what are the values of the environment variables
PATHandPATHEXT, 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.
1. Thanks for the detailed explanation of the if-else syntax. It will be very useful for me.
– giladpac
Dec 6 '15 at 14:55
add a comment |
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 by a space from second compare string.
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 "directoryfile" (echo exists) else (adb push "file" "directory")
Everything on one line with more spaces for easier reading:
if exist "directoryfile" ( 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 "directoryfile" (
echo exists
) else adb push "file" "directory"
Each part of the entire condition on separate lines:
if exist "directoryfile" (
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 2 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 .sdcardfile.any works at all. The question does not contain any information about
- Android version,
- the Android device itself,
- how the device is configured regarding to accessing files and directories on its storage medias – as removable storage(s) making it possible to access the files and directories from Windows command line environment or just via Media Transfer Protocol which is not supported from Windows command line environment,
- in which environment this batch file is running, i.e. what is the current directory, what are the values of the environment variables
PATHandPATHEXT, 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.
1. Thanks for the detailed explanation of the if-else syntax. It will be very useful for me.
– giladpac
Dec 6 '15 at 14:55
add a comment |
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 by a space from second compare string.
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 "directoryfile" (echo exists) else (adb push "file" "directory")
Everything on one line with more spaces for easier reading:
if exist "directoryfile" ( 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 "directoryfile" (
echo exists
) else adb push "file" "directory"
Each part of the entire condition on separate lines:
if exist "directoryfile" (
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 2 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 .sdcardfile.any works at all. The question does not contain any information about
- Android version,
- the Android device itself,
- how the device is configured regarding to accessing files and directories on its storage medias – as removable storage(s) making it possible to access the files and directories from Windows command line environment or just via Media Transfer Protocol which is not supported from Windows command line environment,
- in which environment this batch file is running, i.e. what is the current directory, what are the values of the environment variables
PATHandPATHEXT, 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.
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 by a space from second compare string.
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 "directoryfile" (echo exists) else (adb push "file" "directory")
Everything on one line with more spaces for easier reading:
if exist "directoryfile" ( 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 "directoryfile" (
echo exists
) else adb push "file" "directory"
Each part of the entire condition on separate lines:
if exist "directoryfile" (
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 2 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 .sdcardfile.any works at all. The question does not contain any information about
- Android version,
- the Android device itself,
- how the device is configured regarding to accessing files and directories on its storage medias – as removable storage(s) making it possible to access the files and directories from Windows command line environment or just via Media Transfer Protocol which is not supported from Windows command line environment,
- in which environment this batch file is running, i.e. what is the current directory, what are the values of the environment variables
PATHandPATHEXT, 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.
edited Dec 27 '18 at 19:59
answered Dec 6 '15 at 14:28
Mofi
27.7k83777
27.7k83777
1. Thanks for the detailed explanation of the if-else syntax. It will be very useful for me.
– giladpac
Dec 6 '15 at 14:55
add a comment |
1. Thanks for the detailed explanation of the if-else syntax. It will be very useful for me.
– giladpac
Dec 6 '15 at 14:55
1. Thanks for the detailed explanation of the if-else syntax. It will be very useful for me.
– giladpac
Dec 6 '15 at 14:55
1. Thanks for the detailed explanation of the if-else syntax. It will be very useful for me.
– giladpac
Dec 6 '15 at 14:55
add a comment |
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.
The idea is good. I don't see any reason for using delayed expansion with the missing linesetlocal EnableDelayedExpansionat top. And I think the command FIND is also not needed as it could be enough as first lineset" FileFound=", as second linefor /F %%I in ('adb shell ls "the file"') do set "FileFound=1"and as third lineif defined FileFound (. But I don't haveadbinstalled and no mobile device connected to withadband so don't know whatadb shell ls "the file"outputs on file existing or file missing to verify the command lines as suggested here.
– Mofi
Apr 24 '18 at 5:23
add a comment |
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.
The idea is good. I don't see any reason for using delayed expansion with the missing linesetlocal EnableDelayedExpansionat top. And I think the command FIND is also not needed as it could be enough as first lineset" FileFound=", as second linefor /F %%I in ('adb shell ls "the file"') do set "FileFound=1"and as third lineif defined FileFound (. But I don't haveadbinstalled and no mobile device connected to withadband so don't know whatadb shell ls "the file"outputs on file existing or file missing to verify the command lines as suggested here.
– Mofi
Apr 24 '18 at 5:23
add a comment |
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.
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.
answered Apr 23 '18 at 20:32
demo7up
467
467
The idea is good. I don't see any reason for using delayed expansion with the missing linesetlocal EnableDelayedExpansionat top. And I think the command FIND is also not needed as it could be enough as first lineset" FileFound=", as second linefor /F %%I in ('adb shell ls "the file"') do set "FileFound=1"and as third lineif defined FileFound (. But I don't haveadbinstalled and no mobile device connected to withadband so don't know whatadb shell ls "the file"outputs on file existing or file missing to verify the command lines as suggested here.
– Mofi
Apr 24 '18 at 5:23
add a comment |
The idea is good. I don't see any reason for using delayed expansion with the missing linesetlocal EnableDelayedExpansionat top. And I think the command FIND is also not needed as it could be enough as first lineset" FileFound=", as second linefor /F %%I in ('adb shell ls "the file"') do set "FileFound=1"and as third lineif defined FileFound (. But I don't haveadbinstalled and no mobile device connected to withadband so don't know whatadb shell ls "the file"outputs on file existing or file missing to verify the command lines as suggested here.
– Mofi
Apr 24 '18 at 5:23
The idea is good. I don't see any reason for using delayed expansion with the missing line
setlocal EnableDelayedExpansion at top. And I think the command FIND is also not needed as it could be enough as first line set" FileFound=", as second line for /F %%I in ('adb shell ls "the file"') do set "FileFound=1" and as third line if defined FileFound (. But I don't have adb installed and no mobile device connected to with adb and so don't know what adb shell ls "the file" outputs on file existing or file missing to verify the command lines as suggested here.– Mofi
Apr 24 '18 at 5:23
The idea is good. I don't see any reason for using delayed expansion with the missing line
setlocal EnableDelayedExpansion at top. And I think the command FIND is also not needed as it could be enough as first line set" FileFound=", as second line for /F %%I in ('adb shell ls "the file"') do set "FileFound=1" and as third line if defined FileFound (. But I don't have adb installed and no mobile device connected to with adb and so don't know what adb shell ls "the file" outputs on file existing or file missing to verify the command lines as suggested here.– Mofi
Apr 24 '18 at 5:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33994369%2fbatch-scripting-if-exist-sdcard-file-any-using-adb%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Have you tried this:
if exist ./sdcard/file.any do (echo "exists") else (adb push file.any ./sdcard/)(one-liner)?– aschipfl
Nov 30 '15 at 10:26
Results in: "else was unexpected at this time"
– giladpac
Dec 1 '15 at 13:07
You need to put all this in a songle line! try also to put
""around./sdcard/file.any; moreover I recommend` as path separator rather than/`...– aschipfl
Dec 1 '15 at 13:16