我在 Windows 7 上有一个 which.bat,
@echo off
REM This bat searches a file in PATH list to see whether a file can be found.
REM If found, it shows the file's full path.
REM which.bat gcc.exe
REM shows
REM gcc.exe is found: D:\GMU\MinGW2\bin\gcc.exe
REM
REM Note: Filename extension is significant in the search. E.g. If you run
REM which.bat gcc
REM gcc.exe will not be matched.
IF "%1" == "" goto END
IF "%~$PATH:1" == "" (
echo %1 is not found in any directories from PATH env-var.
) ELSE (
echo %1 is found: %~$PATH:1
)
:END
在我今天发现一个奇怪的行为之前,这只蝙蝠效果很好。
有一个文件 O:\temp\pfiles (x86)\mystuff.txt
,并且 PATH 有内容:
PATH=O:\temp\pfiles (x86);D:\CmdUtils
运行which mystuff.txt
,我得到了非常奇怪的输出:
\mystuff.txt was unexpected at this time.
经过一番摸索,我发现(x86)
in 目录名称导致了问题。要解决方法,我必须在 中添加引号echo
,如下所示:
echo %1 is found: "%~$PATH:1"
这种调整的缺点是显而易见的:引号被打印到屏幕上,这在程序员看来并不总是需要的。
谁能帮助解释这种奇怪的行为?
我发现这个问题是因为在我的真实环境中,我有一些类似C:\Program Files (x86)\Common Files\NetSarang
PATH 的路径,它们表现出完全相同的症状。