Page 1 of 1
how to programmatically escape percent signs %
Posted: 25 Jun 2015 16:52
by thebrenda
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: 26 Jun 2015 01:02
by jeb
In this way you can't escape them later, as the percents signs are already lost in the definition lines.
But you could use a seond file to store the credentials and read them from batch
Your credential file would look like
Code: Select all
--uid=theb%renda
--pwd=pass%word1%234
And in your batch file you need
Code: Select all
< credential.key (
set /p uid=
set /p pwd=
)
"%JAVA_HOME%" -classpath %CLASSPATH% javaprogram "%uid%" "%pwd%"
It could be a good idea to use delayed expansion here, else you get problems with quotes and other special characters.
setlocal EnableDelayedExpansion
Code: Select all
< credential.key (
set /p uid=
set /p pwd=
)
"%JAVA_HOME%" -classpath %CLASSPATH% javaprogram "!uid!" "!pwd!"
Re: how to programmatically escape percent signs %
Posted: 26 Jun 2015 06:39
by thebrenda
get problems with quotes and other special characters
Is there a list of special characters that will cause a problem, and how to escape them?
Also, is there not some code that can search within a variable for the special characters and replace them with the escape and special character?
I did not understand your response, sorry. My dos batch skills are not good.
Re: how to programmatically escape percent signs %
Posted: 27 Jun 2015 07:38
by thebrenda
Can anyone help? I need to remove any special characters that will cause a problem from my variable. I am willing to try any solutions. Anyone have a link or code examples?
Re: how to programmatically escape percent signs %
Posted: 27 Jun 2015 09:56
by jeb
Special characters are more or less
You can escape any character with a caret, only the percent can only be escaped with another percent.
Quotes toggles the mode of the following characters, all (but percent) are handled as normal characters.
thebrenda wrote:Also, is there not some code that can search within a variable for the special characters and replace them with the escape and special character?
Yes, the syntax is
Code: Select all
%variable:search=replace%
or
!variable:search=replace!
The second sample uses delayed expansion instead of percent expansion.
So you could build something like this
Code: Select all
set var=cat^&dog
set var
echo "%var%"
echo %var:&=^&%
But it your sample this doesn't help
Code: Select all
set uid="--uid=theb%renda"
set pwd="--pwd=pass%word1%234"
set uid
set pwd
Output:
uid="--uid=thebrenda"
pwd="--pwd=pass234"
The set <variablename> lines shows simply the content of a variable without modify it.
So you can see here that in your variables isn't any percent, so there isn't anything left you can search and replace for.
Re: how to programmatically escape percent signs %
Posted: 28 Jun 2015 17:30
by thebrenda
starting a new thread. not getting far with this as I guess my original premise was flawed.