Page 1 of 1

how to programmatically escape percent signs %

Posted: 11 Sep 2018 04:09
by sage12
I have a .bat file that passes in connection strings and password arguements to a java program. Any of these fields could have embedded percent signs. The values are entered by the client. I would like to programmatically search these fields and replace all % with %%. Below is an exmple where two arguments (uid and pwd) have embedded %. When the java program receives the arguments they are not as written in the dos batch file.

set uid="--uid=theb%renda"
set pwd="--pwd=pass%word1%234"

"%JAVA_HOME%" -classpath %CLASSPATH% javaprogram %uid% %pwd%

want to turn uid and pwd to this

set uid="--uid=theb%%renda"
set pwd="--pwd=pass%%word1%%234"

"%JAVA_HOME%" -classpath %CLASSPATH% javaprogram %uid% %pwd%

Re: how to programmatically escape percent signs %

Posted: 11 Sep 2018 05:42
by jeb
Hi sage12,

you should use delayed expansion, too avoid any problems with special charaters.

Code: Select all

setlocal EnableDelayedExpansion
"%JAVA_HOME%" -classpath %CLASSPATH% javaprogram !uid! !pwd!
But your main problem is a bit difficult.
I don't understand, who builds the lines

Code: Select all

set uid="--uid=theb%renda"
set pwd="--pwd=pass%word1%234"
Are they build by another program and prepended to your batch file?
Or are they part of another batch file?

This way the set command will drop the percent signs.
But when the it's part of a config/user file, you could read that file via FOR/F without losing the percents.