how to programmatically escape percent signs %

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sage12
Posts: 1
Joined: 11 Sep 2018 04:08

how to programmatically escape percent signs %

#1 Post by sage12 » 11 Sep 2018 04:09

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: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: how to programmatically escape percent signs %

#2 Post by jeb » 11 Sep 2018 05:42

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.

Post Reply