41

I am trying to set up a TeamCity build process that runs a custom command line script. The script uses a variable so it needs a percent sign (e.g. %x). But TeamCity uses percent signs for its properties (e.g. %build.number%), so the percent sign in the script gets removed when it runs.

If the script contains this:

for /d %x in ("c:\*") do @echo "%x"

This is what it actually runs:

for /d x in ("\*") do @echo "x"

How can I write my script so it can include variables?

tspauld
  • 3,512
  • 2
  • 25
  • 22

3 Answers3

70

If you want to pass % to TeamCity, you should escape it with another %, i.e. for % it must be %%`.

But the Windows command line considers % as an escape character, so you should escape it again adding another % before each %, i.e. for %% you should pass %%%%

Flow is:

%%%% in cmd -> %% in TeamCity -> % actual sign.

tl;dr: the answer to your question will be:

for /d %%%%x in ("c:\*") do @echo "%%%%x"
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Marat Turaev
  • 797
  • 5
  • 5
  • Hi Marat, your answer has helped me to escape % in command line build step. But I still don't understand why do i need to pass 4 % signs, when %% is supposed to escape the sign? Do you have any link to any document explaining this ? – Newbee Jul 01 '14 at 10:42
  • 2
    @Newbee There are 2 layers of escapes. The first being TeamCity, the second is the command line. So, if you pass %%%% into TeamCity, it interprets it as %% and passes it to the command line. The command line then takes the %% and interprets it as %. – xubia Jul 08 '15 at 16:33
35

Try for /d %%x in ("c:\*") do @echo "%%x" (i.e. duplicate the % signs).

But there should be a way to tell TC to leave the file alone. It would be horrible if TC would remove the percent signs in the sources. Therefore, I'm pretty sure that you did something in the configuration to enable replacement of %.

On a similar note, is it really TC that messes with the script? Or are you using a build tool to generate the script or something like that?

Richard Ev
  • 52,939
  • 59
  • 191
  • 278
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • I was originally trying to use a custom script instead of a separate file, and I tried the duplicate percent signs that way but it did not work. Using them in a separate file works though. – tspauld Dec 08 '10 at 17:12
  • 7
    Also, I was able to get it working as a custom script by tripling the percent signs. – tspauld Dec 08 '10 at 17:17
  • Still there must be a reason why your script gets corrupted. Which program expands `%build.number%`? Is it really TC? If so, then there must be an option to turn it off. If you need the build number, I suggest to put that into a different script and read it will `call`. – Aaron Digulla Dec 09 '10 at 09:02
  • 3
    OH MY GOD! I've spent the whole day trying to figure it out. Thank you SO much! – Allan Spreys Jan 20 '14 at 03:53
4

It seems that TeamCity simply sticks what you enter into a .cmd file The for statement requires double percents in these cases. It then seems that TeamCity removes one of these % signs, hence why tspauld got it to work (this is also how i got it to run).

In the logs it seems that TeamCity creates a file here Program Files\TeamCity\buildAgent\temp\agentTmp but the for cmd executes and dies too soon to see what it has written, presumably if the first line was a long executing task you would be able to check this (annoyingly 'pause' didn't work).

Rohan
  • 52,392
  • 12
  • 90
  • 87
Verdon
  • 41
  • 1