how to programmatically escape percent signs %

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
thebrenda
Posts: 7
Joined: 25 Jun 2015 13:21

how to programmatically escape percent signs %

#1 Post by thebrenda » 25 Jun 2015 16:52

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%

jeb
Expert
Posts: 1042
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: how to programmatically escape percent signs %

#2 Post by jeb » 26 Jun 2015 01:02

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!"

thebrenda
Posts: 7
Joined: 25 Jun 2015 13:21

Re: how to programmatically escape percent signs %

#3 Post by thebrenda » 26 Jun 2015 06:39

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.

thebrenda
Posts: 7
Joined: 25 Jun 2015 13:21

Re: how to programmatically escape percent signs %

#4 Post by thebrenda » 27 Jun 2015 07:38

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?

jeb
Expert
Posts: 1042
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: how to programmatically escape percent signs %

#5 Post by jeb » 27 Jun 2015 09:56

Special characters are more or less

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.

thebrenda
Posts: 7
Joined: 25 Jun 2015 13:21

Re: how to programmatically escape percent signs %

#6 Post by thebrenda » 28 Jun 2015 17:30

starting a new thread. not getting far with this as I guess my original premise was flawed.

Post Reply