batch scripting - if exist ./sdcard/file.any using adb












0














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/









share|improve this question




















  • 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
















0














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/









share|improve this question




















  • 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














0












0








0


1





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/









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












2 Answers
2






active

oldest

votes


















1














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.





  1. Everything on one line with as less spaces as possible (hard to read):



    if exist "directoryfile" (echo exists) else (adb push "file" "directory")



  2. Everything on one line with more spaces for easier reading:



    if exist "directoryfile" ( echo exists ) else ( adb push "file" "directory" )



  3. 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"



  4. 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 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.






share|improve this answer























  • 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



















0














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.






share|improve this answer





















  • 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











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
});


}
});














draft saved

draft discarded


















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









1














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.





  1. Everything on one line with as less spaces as possible (hard to read):



    if exist "directoryfile" (echo exists) else (adb push "file" "directory")



  2. Everything on one line with more spaces for easier reading:



    if exist "directoryfile" ( echo exists ) else ( adb push "file" "directory" )



  3. 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"



  4. 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 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.






share|improve this answer























  • 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














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.





  1. Everything on one line with as less spaces as possible (hard to read):



    if exist "directoryfile" (echo exists) else (adb push "file" "directory")



  2. Everything on one line with more spaces for easier reading:



    if exist "directoryfile" ( echo exists ) else ( adb push "file" "directory" )



  3. 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"



  4. 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 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.






share|improve this answer























  • 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












1








1






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.





  1. Everything on one line with as less spaces as possible (hard to read):



    if exist "directoryfile" (echo exists) else (adb push "file" "directory")



  2. Everything on one line with more spaces for easier reading:



    if exist "directoryfile" ( echo exists ) else ( adb push "file" "directory" )



  3. 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"



  4. 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 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.






share|improve this answer














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.





  1. Everything on one line with as less spaces as possible (hard to read):



    if exist "directoryfile" (echo exists) else (adb push "file" "directory")



  2. Everything on one line with more spaces for easier reading:



    if exist "directoryfile" ( echo exists ) else ( adb push "file" "directory" )



  3. 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"



  4. 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 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.







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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













0














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.






share|improve this answer





















  • 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
















0














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.






share|improve this answer





















  • 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














0












0








0






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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 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
















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'