I'm trying to implement, in a Windows batch file, the logic "if %MyBinaryDir%
is not already at the beginning of the system %PATH%
, then put it there; if it's already there, do nothing".
I've got this:
@echo %PATH% | findstr /i /b /c:"%MyBinaryDir%;" > nul || set "PATH=%MyBinaryDir%;%PATH%
This has always worked pretty well, until I tried to deploy on someone's machine where for some unearthly reason the %PATH%
variable contained an odd number of quote characters.
The problem boils down to this:
@set x="
@echo %x%
@echo %x% | more
The second line prints a single "
character. The third line is tripped up by the quote character and fails to pipe the echo
output to the second binary (in my case findstr
, but in this boiled-down example more
) at all. Instead, it literally prints the characters:
" | more
So my questions are:
(Y) How do I safely pipe any arbitrary string into a second binary?
and/or
(X) Is there a way of conditionally-prepending directories to the system path that avoids this mess?