Windows .bat file, %~$PATH:1 quotes issue -


i have which.bat on windows 7,

@echo off rem bat searches file in path list see whether file can found. rem if found, shows file's full path. rem     which.bat gcc.exe rem shows rem     gcc.exe found: d:\gmu\mingw2\bin\gcc.exe rem  rem note: filename extension significant in search. e.g. if run rem     which.bat gcc rem gcc.exe not matched.  if "%1" == "" goto end  if "%~$path:1" == "" (       echo %1 not found in directories path env-var.     ) else (       echo %1 found: %~$path:1     )  :end 

this bat works until find strange behavior today.

there file o:\temp\pfiles (x86)\mystuff.txt, , path has content:

path=o:\temp\pfiles (x86);d:\cmdutils 

running which mystuff.txt, got very strange output:

\mystuff.txt unexpected @ time. 

enter image description here

after poking around, find (x86) in directory name causes problem. workaround, have add quotes echo, this:

echo %1 found: "%~$path:1" 

the downside of such tweak obvious: quotes printed screen not desired in programmer's opinion.

can explain strange behavior?

i find problem because in real env, have paths c:\program files (x86)\common files\netsarang in path, exhibit same symptom.

enter image description here

ms dos pretty simple shell implementation, , have figured out interpretation of 1 dos command line goes in 2 phases:

  1. evaluation of variables in current line
  2. interpretation of evaluated command line

in case command line:

if "%~$path:1" == "" (       echo %1 not found in directories path env-var.     ) else (       echo %1 found: %~$path:1     ) 

would interpreted as:

if "o:\temp\pfiles (x86)\mystuff.txt" == "" (       echo mystuff not found in directories path env-var.     ) else (       echo mystuff.txt found: o:\temp\pfiles (x86)\mystuff.txt     ) 

now can notice problem in (x86), i.e. interpreter sees somehow - first ) closes else statement:

) else (       echo mystuff.txt found: o:\temp\pfiles (x86 )\mystuff.txt ) 

solution: put "" around potentially problematic variables.

i put quotes around whole echo command content, example:

echo "%1 found: %~$path:1" 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -