這個問題擴展了對this deleted answer的評論。我聲稱PATH中未擴展的變量引用在搜索可執行文件時不會被擴展,但Ken說他沒有看到我所做的同樣的行爲。搜索PATH時擴展了環境變量嗎?
請注意,普通情況微妙但截然不同:根據特定條件,當PATH環境變量由註冊表中的信息構建時,環境變量會自動擴展。我在談論這種情況,由於某種原因,這種情況沒有發生,所以cmd.exe
進程的實際環境塊包含一個PATH,其中仍然包含環境變量引用。
這裏是我建,以測試這種行爲的代碼:
md test1
echo echo hello! > test1\test1.cmd
set TESTPATH=%cd%\test1
set percent=%%
set PATH=c:\windows\system32;c:\windows;c:\windows\system32\Wbem;%percent%TESTPATH%percent%
set PATH
set TESTPATH
test1
cmd /c test1
start test1.cmd
,這是我的機器上的結果:
C:\working\testpath>test
C:\working\testpath>md test1
C:\working\testpath>echo echo hello! 1>test1\test1.cmd
C:\working\testpath>set TESTPATH=C:\working\testpath\test1
C:\working\testpath>set percent=%
C:\working\testpath>set PATH=c:\windows\system32;c:\windows;c:\windows\system32\
Wbem;%TESTPATH%
C:\working\testpath>set PATH
Path=c:\windows\system32;c:\windows;c:\windows\system32\Wbem;%TESTPATH%
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.py;.pyw
C:\working\testpath>set TESTPATH
TESTPATH=C:\working\testpath\test1
C:\working\testpath>test1
'test1' is not recognized as an internal or external command,
operable program or batch file.
C:\working\testpath>cmd /c test1
'test1' is not recognized as an internal or external command,
operable program or batch file.
C:\working\testpath>start test1.cmd
The system cannot find the file test1.cmd.
什麼是預期的行爲?它取決於Windows的版本和/或其他因素嗎?
很好地完成。我想我們都是對的。 :-)(不是關於我發佈的回答鏈接的問題是不正確的 - 你對這方面是正確的。) –
@KenWhite:我的測試代碼對你的行爲不同於它對我的? (如果沒有,也許我剛剛誤解了你的意思?) –
明天我纔有機會測試它。看起來你在調查方面做得很好;你會很容易做到這一點。 –