Command Line Build Step with "%%" Character?
I am trying to implement the following custom build step using the "Command Line" build step option. I have defined a "Configuration Parameter" named "mydir" that should be evaluated by TeamCity but the remainder of the "%" symbols need to be left alone.
del /q "%mydir%\*"
for /d %%x in ("%mydir%\*") do @rd /s /q ^"%x^"
I cannot figure out how to properly escape the "%" characters in order to get the output expected above. I have tried numerous combinations but they always fail to evaluate properly. Any help would be greatly appreciated.
Please sign in to leave a comment.
Hi Keith,
To escape "%" use "%%". Every occurrence of "%%" in the values where property references are supported will be replaced to "%" before passing the value to the build. For more details please read about Build Parameters.
I tried that. It doesn't work as I expected. We are using TeamCity v8.1.3 (build 30101). Here is the result that I get back when I do this... It looks like it is not processing the "%%" as a standard escape character and is instead evaluating everything inside the outermost "%%" (or something like that) which causes issues.
Input:
del /q "%mydir%\*"
for /d %%%%x in ("%mydir%\*") do @rd /s /q ^"%%x^"
Actual Output:
del /q "C:\mydir\*"
for /d %x in ("C:\mydir\*") do @rd /s /q ^"x"
Expected Output:
del /q "C:\mydir\"
for /d %%x in ("C:\mydir\*") do @rd /s /q ^"%x^"
Sorry for delay. Windows command line considers % as an escape character. So you should escape it again adding another % before each %. %%%% in cmd = %% in TeamCity = % actual sign.
The other solution is to switch to "Executable with parameters" mode.
Thank you so much!!! This finally works. So basically if you want to place a "%" in the processed output you have to place "%%%%" in the input string. I have provided the final formatted command below for reference in case anyone else ever need this.
Configuration Parameters:
mydir => C:\mydir
Input:
del /q "%mydir%\*"
for /d %%%%%%%%x in ("%mydir%\*") do @rd /s /q ^"%%%%x^"
Output:
del /q "C:\mydir\"
for /d %%x in ("C:\mydir\*") do @rd /s /q ^"%x^"