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%
how to programmatically escape percent signs %
Moderator: DosItHelp
Re: how to programmatically escape percent signs %
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
And in your batch file you need
It could be a good idea to use delayed expansion here, else you get problems with quotes and other special characters.
setlocal EnableDelayedExpansion
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 %
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 %
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 %
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.
Yes, the syntax is
The second sample uses delayed expansion instead of percent expansion.
So you could build something like this
But it your sample this doesn't help
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.
Code: Select all
<>|&"<>^!%
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 %
starting a new thread. not getting far with this as I guess my original premise was flawed.