2016-08-14 104 views
1

我可以通過使用%〜dp0來獲得當前的bat文件路徑。如何在Windows BAT中獲得程序完整路徑?

但如何將python.exe轉換爲「C:\ Python27 \ python.exe」?

+1

Perhpas你的意思是這樣==> [如何找到完整路徑批處理腳本的應用程序( http://stackoverflow.com/questions/1437457/how-do-i-find-full-path-to-an-application-in-a-batch-script) – Hackoo

+0

_where_命令可以幫助你。 **在命令提示符下輸入/?**。接下來,您可以使用_for_循環獲取結果 – elzooilogico

+0

呃,您發佈該評論而不是答案...不知道是否在同一時間我編輯了我的答案。 – TylerY86

回答

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% 
+0

不,這是bat文件的路徑,我的意思是如何獲得命令程序的完整路徑....作爲一個字符串 –

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