I have who.bat on Windows 7,
@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 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. Eg 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
This bat works well until I find strange behavior today.
There is a file O:\temp\pfiles (x86)\mystuff.txt , and PATH has the content:
PATH=O:\temp\pfiles (x86);D:\CmdUtils
Running which mystuff.txt , I got the output of VERY STRANGE :
\mystuff.txt was unexpected at this time.

After some searching, I found that the name (x86) in the directory was causing the problem. To get a workaround, I have to add quotes to echo , for example:
echo %1 is found: "%~$PATH:1"
The disadvantage of this setting is obvious: quotation marks are printed on the screen, which is not always desirable in the opinion of the programmer.
Can someone explain this weird behavior?
I find this problem because in my real env I have several paths, such as C:\Program Files (x86)\Common Files\NetSarang in PATH, which exhibit exactly the same symptom.

source share