2

We have a Windows Server 2008 R2 machine that has a directory that holds all of the third party libraries our dev team uses. I set up a system wide environment variable called 3P_Libs. From cmd prompt I can see the correct value:

D:\_AMG_Integration_\_NightlyBuild_>echo %3P_Libs%
D:\_third_party_libraries_

I have a nightly build script that references this path when building but it throws an error because the '3' is dropped leaving just the value of P_Libs instead of the correct path. I added an echo of the value from the script:

D:\_AMG_Integration_\_NightlyBuild_>echo P_Libs
P_Libs

It appears that the batch file is interpreting %3 on its own as the 3rd input variable despite the fact that no input has been provided to the script. Is there something that needs to be done to escape the '3'?

jterm
  • 973
  • 1
  • 12
  • 32
  • 3
    In a batch file, `%3` means "the third command line parameter". – Raymond Chen Dec 09 '13 at 22:30
  • 1
    Why don't you simply change the Var name in $3P_libs for exemple ? – SachaDee Dec 10 '13 at 01:12
  • I mentioned in the question that %3 is the third parameter. Yes I can rename it but my question was can this value be escaped so I don't have to update everyone's references to this path just so my script will work? – jterm Dec 10 '13 at 14:50

2 Answers2

3

As Raymond Chen pointed the reason is that the script takes the %3 as the third parameters passed to it. The only ways that come to me at the moment to fix this is using delayed expansion or for /f:

setlocal enableDelayedExpansion
echo !3P_Libs!
endlocal

for /f:

for /f "tokens=2* delims==" %%a in ('set 3P_Libs') do echo %%a
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • if it is only to be echoed: `set 3P_Libs` will work. The problem is with `echo`. `C:\> %3P_Lib%` gives the right path (of course with an error, because its a path, not a program. – Stephan Dec 10 '13 at 09:18
2

You can do it like this:

setlocal enableDelayedExpansion
echo !3P_Libs!

See: How does the Windows Command Interpreter (CMD.EXE) parse scripts?

Community
  • 1
  • 1
Myforwik
  • 3,438
  • 5
  • 35
  • 42