我有一个在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)
导致了问题。为了解决这个问题,我不得不在echo
命令中添加引号,像这样:
echo %1 is found: "%~$PATH:1"
这种调整的缺点显而易见:引号被打印到屏幕上,这在程序员看来并不总是理想的。有人能帮忙解释一下这种奇怪的行为吗?我发现这个问题是因为在我的真实环境中,我有一些路径,如
C:\Program Files (x86)\Common Files\NetSarang
在 PATH 中,出现了完全相同的症状。
where.exe
非常好用。自从Windows XP时期还没有where.exe
,我一直在使用which.bat
。 - Jimm Chen