I'm trying to install an app from my PC running Windows 10 using adb and I need to use command cd
before I'm installing the app.
But I see a strange behavior. If I use the cd
command before the if
statement, it's working well. But if I use the cd
command inside the if
statement, it is not working.
Here is what I tried:
This one is working well:
cd %~f0\..\..\apps\app_sample\samples\sample_app\build\outputs\apk if "%arg1%" == "aaa" ( adb install %CD%\sample_app-debug.apk )
This one results on execution in an error:
if "%arg1%" == "aaa" ( cd %~f0\..\..\apps\app_sample\samples\sample_app\build\outputs\apk adb install %CD%\sample_app-debug.apk )
The error output is:
Failed to stat
C:\Users\ntuser\Documents\workspace\Team\script\sample_app-debug.apk:
No such file or directoryIt looks like it did not do the
cd
command in this case.
Whenever Windows command interpreter
cmd.exe
encounters the beginning of a command block starting with(
and ending with matching)
, the entire block is preprocessed before the command left to the command block is executed at all. This means all environment variables using%variable%
syntax are replaced by current value of the referenced environment variable before executing the command which later executes the entire command block. This behavior can be seen on running a batch file from within a command prompt window without@echo off
being usually at top of the batch file.This means on your batch file with the IF command and the command block to execute on condition being true that
%CD%
is replaced by current directory before the command IF is executed and so also before the command CD is executed at all.There are two possibilities:
!variable!
or
Solution 1 with enabled delayed environment variable expansion:
Solution 2 with not using a command block at all and instead use GOTO:
Another solution 2 is using a subroutine and command CALL:
But I think your code can be even more easier because it is most likely not necessary to specify the *.apk file with full path.
And perhaps also working is:
It is better to use here
%~dp0
which references the drive and path of the batch file ending with a backslash instead of%~f0
which is the name of the batch file with file extension and full path.For example with batch file being
C:\Temp\Test\Install.bat
the command lineresults in execution of
and the current directory is
C:\Temp\apps\app_sample\samples\sample_app\build\outputs\apk
after this command with in real invalid directory nameInstall.bat
in path which is not a directory.The command line
is much better as it results in execution of
which makes also
C:\Temp\apps\app_sample\samples\sample_app\build\outputs\apk
the current directory, but with a completely correct path in comparison to abovecd
command line.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.
call /?
... explains%~dp0
and%~f0
.cd /?
endlocal /?
goto /?
if /?
rem /?
set /?
... explains delayed environment variable expansion on an IF and a FOR example.setlocal /?
I don't have adb installed. So I can only assume that it is an executable with file extension
.exe
and not a batch file with file extension.bat
or.cmd
which would require the usage ofcall adb.bat ...
orcall adb.cmd ...
in this batch file.