如何更改IF语句中的当前目录并使用环境变量CD引用其路径? [英] How to change current directory inside IF statement and reference its path with environment variable CD?

查看:181
本文介绍了如何更改IF语句中的当前目录并使用环境变量CD引用其路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 adb 从运行Windows 10的PC上安装应用程序,我需要在我之前使用命令 cd 安装应用程序。



但我看到一种奇怪的行为。如果我在 if 语句之前使用 cd 命令,那么它运行良好。但是如果我在 if 语句中使用 cd 命令,它就不起作用。



以下是我的尝试:


  1. 这个效果很好:

      cd%~f0 \..\..\apps\app_sample\samples\sample_app\build\outputs \\ \\ apk 
    如果%arg1%==aaa(
    adb install%CD%\ _sample_app-debug.apk


  2. 这个导致错误执行:

     如果%arg1%==aaa(
    cd%~f0\..\..\apps\app_sample\samples\sample_app \ build \ output \ apk
    adb install%CD%\ _sample_app-debug.apk

    错误输出为:


    无法统计

    C:\ Users \\\\\\\ Documents\workspace \Team \ script \ _sample_app-debug.apk:

    没有这样的文件或目录


    在这种情况下看起来它没有执行 cd 命令。



解决方案

每当Windows命令解释程序 cmd.exe 遇到以开头的命令块的开头(并以匹配的结尾),整个块在命令之前被预处理完全执行左边的命令块。这意味着在执行稍后执行整个命令块的命令之前,使用%variable%语法的所有环境变量将被引用的环境变量的当前值替换。从命令提示符窗口运行批处理文件时可以看到此行为,而 @echo off 通常位于批处理文件的顶部。



这意味着您的批处理文件中包含 IF 命令,并且在条件为true时执行的命令块为%CD%执行 IF 之前替换为当前目录,在执行 CD 命令之前 所有。



有两种可能性:


  1. 明确启用延迟环境变量扩展并使用语法!variable来引用命令块中的环境变量!


  2. 不要通过使用 GOTO 或子程序和 CALL 来使用命令块。

解决方案1启用延迟环境变量扩展:

  setlo cal EnableDelayedExpansion 
如果%arg1%==aaa(
cd%~dp0..\apps\app_sample\samples\sample_app\build \ outputs \ apk
adb.exe install!CD!\ _sample_app-debug.apk

endlocal

解决方案2完全没有使用命令块而是使用 GOTO

 如果%arg1%==aaagoto AAA 
rem其他命令
goto:EOF

:AAA
cd% ~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk
adb.exe install%CD%\ _sample_app-debug.apk
转到:EOF
rem或者使用不同的GOTO继续处理批处理文件中的其他位置。

另一个解决方案2是使用子程序并命令 CALL

 如果%arg1%==aaa调用:AAA 
rem即使上述条件为真,也会执行其他命令。

rem避免跌入下面的子程序。
goto:EOF

:AAA
cd%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk
adb.exe install%CD%\ _sample_app-debug.apk
rem退出子程序并在下面继续调用它。
goto:EOF

但我认为你的代码可以更容易,因为它最多可能没有必要使用完整路径指定* .apk文件。

 如果%arg1%==aaa( 
cd%~dp0..\apps \app_sample \samples \sample_app \ build \ output> \\ _apk
adb.exe install sample_app-debug.apk

也许还有效:

 如果%arg1%==aaaadb.exe安装%~dp0..\apps \app_sample \samples \ _sample_app \ build \ output \ \\ apk \ _sample_app-debug.apk






最好在这里使用%~dp0 ,它引用以反斜杠结尾的批处理文件的驱动器和路径,而不是%~f0 这是具有文件扩展名和完整路径的批处理文件的名称。



例如,批处理文件为 C:\ Temp\Test\I nstall.bat 命令行

  cd%~f0 \..\ .. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ >导致执行

  cdC:\ Temp\Test \Install.bat \ .. \ .. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 

并且当前目录是 C:\Temp \apps \app_sample \samples \sample_app \ build \ output> \\ _apk 在此命令之后与实际无效目录名 Install.bat 在不是目录的路径中。



命令行

  cd%~dp0..\apps\app_sample\samples\sample_app\build\outputs\APK 

更好,因为它导致执行

  cdC:\Temp\Test \\ .. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 

这也是 C:\Temp \apps \ app_sample \samples \sample_app \ build \ output> \\ _apk 当前目录,但与上面的 cd 命令行相比,路径完全正确。



用于理解使用的命令及其工作原理,打开一个命令提示符窗口,执行以下命令,并非常仔细地读取为每个命令显示的所有帮助页面。




  • call /? ...解释%~dp0 %~f0

  • cd /?

  • endlocal /?

  • 转到/?

  • if /?

  • rem /?

  • set /? ...解释 IF FOR example。

  • setlocal /?



我没有安装 adb 。所以我只能假设它是一个文件扩展名为 .exe 的可执行文件,而不是文件扩展名为 .bat 或 .cmd 这需要使用调用adb.bat ... 在此批处理文件中调用adb.cmd ...


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:

  1. 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
    )
    

  2. 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 directory

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

  1. explicitly enable delayed environment variable expansion and reference the environment variable in the command block with using syntax !variable!
    or
  2. don't use a command block by using GOTO or a subroutine and CALL.

Solution 1 with enabled delayed environment variable expansion:

setlocal EnableDelayedExpansion
if "%arg1%" == "aaa" (
    cd "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk"
    adb.exe install "!CD!\sample_app-debug.apk"
)
endlocal

Solution 2 with not using a command block at all and instead use GOTO:

if "%arg1%" == "aaa" goto AAA
rem Other commands
goto :EOF

:AAA
cd "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk"
adb.exe install "%CD%\sample_app-debug.apk"
goto :EOF
rem Or use a different GOTO to continue processing somewhere else in batch file.

Another solution 2 is using a subroutine and command CALL:

if "%arg1%" == "aaa" call :AAA
rem Other commands executed even if above condition is true.

rem Avoid a fall through to the subroutine below.
goto :EOF

:AAA
cd "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk"
adb.exe install "%CD%\sample_app-debug.apk"
rem Exit the subroutine and continue on line below calling it.
goto :EOF

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.

if "%arg1%" == "aaa" (
    cd "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk"
    adb.exe install sample_app-debug.apk
)

And perhaps also working is:

if "%arg1%" == "aaa" adb.exe install "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk\sample_app-debug.apk"


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 line

cd "%~f0\..\..\apps\app_sample\samples\sample_app\build\outputs\apk"

results in execution of

cd "C:\Temp\Test\Install.bat\..\..\apps\app_sample\samples\sample_app\build\outputs\apk"

and the current directory is C:\Temp\apps\app_sample\samples\sample_app\build\outputs\apk after this command with in real invalid directory name Install.bat in path which is not a directory.

The command line

cd "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk"

is much better as it results in execution of

cd "C:\Temp\Test\..\apps\app_sample\samples\sample_app\build\outputs\apk"

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 above cd 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 of call adb.bat ... or call adb.cmd ... in this batch file.

这篇关于如何更改IF语句中的当前目录并使用环境变量CD引用其路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆