如何在Windows BAT中获得程序完整路径?

时间:2016-08-14 07:43:41

标签: batch-file cmd

我可以通过使用%~dp0来获取当前的bat文件路径。

但是如何将python.exe转换为“C:\ Python27 \ python.exe”?

2 个答案:

答案 0 :(得分:1)

test.bat的

@for %%i in (python.exe) do @set py=%%~$PATH:i
@echo %py%

@for %%i in ( %py%) do @set py=%%~dpi
@echo py's directory is %py%

OneLine bat whereis ==

where.bat

@for %%i in (%*) do @if not "%%~$PATH:i"=="" (echo %%~$PATH:i) else echo %%i

where ls.exe where.bat python.exe
D:\Program Files\Git\usr\bin\ls.exe
E:\sourceCode\shell\where.bat
C:\Python27\python.exe

答案 1 :(得分:0)

两种方式;

call :label python.exe
:label
call %~dp$PATH:1

或者如果您有where可用...

for /f %f in ('where python.exe') do %f

(您可能需要在批处理文件中使用双倍百分比[%%]进行此操作。)

如果路径上有多个可执行文件,则需要类似......

的内容
set target=
for /f %f in ('where python.exe') do (
    set target=%f
)
call %target%
相关问题