Batch Scripting - If Exist ./Sdcard/File.Any Using Adb

Batch Script with Input. ADB commands, FireStick

To those who stumble upon this.

The command:

set /p IPInput = Enter the IP address:

Must not contain spaces before/after the equal sign. It will be as follows:

set /p IPInput=Enter the IP address:

You can then use the command:

adb connect %IPInput%:5555

To connect to your FireStick

The top half of my code that will connect to the FireStick (provided adb and its constituents are in your path) is as follows:

cd %~d0\FireStickAutomation
adb kill-server
adb start-server

timeout 5

@echo off
echo.
echo.
echo .. READ ME ..
echo.
echo.
echo Find the IP of the FireStick; Go to Settings, System, About, Network
echo.
echo.
echo Make note of the entire string
echo.
echo.
set /p IPInput=Enter the IP address including the dots:
echo.
echo.

adb connect %IPInput%:5555

How to correctly check the presence of parameters in batch?

Please read the answers on Why is no string output with 'echo %var%' after using 'set var = text' on command line? for an explanation why it is in general no good idea in batch files to put 1 or more spaces around the equal sign on assigning a string to an environment variable.

And read this answer explaining correct syntax for IF conditions in batch files. Spaces must be used with care in batch files. On some positions a space is required, on other positions a space is often a mistake.

And care must be taken on referencing values of an environment variables within a block defined with ( ... ) as command processor replaces all references to environment variables using %variable% syntax by their values in entire block before processing the lines in block. For an environment variable defined or set within a block and also evaluated within a block it is necessary to enable delayed expansion and reference those variables with !variable! to make used of delayed expansion. Even better is to avoid the need of delayed environment variable expansion.

goto is a keyword rarely used in C/C++. But in batch files it is a very important command and its usage makes most batch files much easier than using nested IF constructs used in C/C++ to avoid usage of goto.

There is a general rule on evaluating the parameters of a batch file:

If first parameter is not defined, there are definitely no more parameters.

Well, it is possible to use for example "" "second parameter" ThirdParameter on calling a batch file to run it with 3 parameters whereby first one is an empty string. But for your script an empty string can be interpreted like parameter not specified at all.

Here is your batch file recoded with usage of goto and avoiding the need of delayed expansion.

@echo off
echo Network Home Script V0.1
setlocal

rem Default configuration

set "NHS_Network1=192.168.0.0/24"
set "NHS_Router1=192.168.0.254"
set "NHS_Network2=192.168.1.0/24"
set "NHS_Router2=192.168.1.1"
set "NHS_Gateway1=192.168.1.1"
set "NHS_Gateway2=192.168.0.254"

rem Run script with the defaults if executed without any
rem parameter or with just 1 parameter for first network.

if "%~1"=="" goto SetRoutes
if "%~2"=="" goto SetRoutes

rem IPv4 address with netmask and router address defined for network 1.

set "NHS_Network1=%~1"
set "NHS_Router1=%~2"

rem Delete all other by default defined environment variables.

set "NHS_Network2="
set "NHS_Router2="
set "NHS_Gateway1="
set "NHS_Gateway2="

if "%~3"=="" goto SetRoutes
if "%~4"=="" goto SetRoutes

rem IPv4 address with netmask and router address defined for network 2.

set "NHS_Network2=%~3"
set "NHS_Router2=%~4"

rem Set the gateway addresses if defined as fifth and sixth parameter.

if not "%~5"=="" set "NHS_Gateway1=%~5"
if not "%~6"=="" set "NHS_Gateway2=%~6"

:SetRoutes

rem The variables NHS_Network1 and NHS_Router1 are always defined.

%SystemRoot%\System32\route.exe delete %NHS_Network1%
%SystemRoot%\System32\route.exe add %NHS_Network1% %NHS_Router1%
if errorlevel 1 (
echo ERROR : Impossible to add route for network 1, please check route print.
) else (
echo Route for network 1 added.
)

rem NHS_Router2 is always defined if NHS_Network2 is defined.

if defined NHS_Network2 (
%SystemRoot%\System32\route.exe delete %NHS_Network2%
%SystemRoot%\System32\route.exe add %NHS_Network2% %NHS_Router2%
if errorlevel 1 (
echo ERROR : Impossible to add route for network 2, please check route print.
) else (
echo Route for network 2 added.
)
)

if defined NHS_Gateway1 (
%SystemRoot%\System32\route.exe delete 0.0.0.0/0
%SystemRoot%\System32\route.exe add 0.0.0.0/0 %NHS_Gateway1% metric 10
if errorlevel 1 (
echo ERROR : Impossible to add route for gateway 1, please check route print.
) else (
echo Route for gateway 1 added.
)
)

if defined NHS_Gateway2 (
%SystemRoot%\System32\route.exe add 0.0.0.0/0 %NHS_Gateway2% metric 20
if errorlevel 1 (
echo ERROR : Impossible to add route for gateway 2, please check route print.
) else (
echo Route for gateway 2 added.
)
)

endlocal

See also the Microsoft support article Testing for a Specific Error Level in Batch Files.

The usage of if errorlevel 1 - meaning if exit code of previous command or application is greater or equal 1 - is better than using %ERRORLEVEL% within a block as command processor replaces %ERRORLEVEL% already on parsing the block by current value of this variable before the line above is executed at all. It would be necessary to use delayed expansion if not using special syntax for checking on error level.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • if /?
  • rem /?
  • route /?
  • set /?
  • setlocal /?

Check if directory exists using ADB and push a file if it does

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


Related Topics



Leave a reply



Submit