Computer Hope
Microsoft => Microsoft DOS => Topic started by: John_L. on September 04, 2012, 02:20:51 PM
-
I am trying to do the following:
SET I=http://www.someURL.com/My%20Webpage%28Version%20B%29.html
in a batch file. However, the second parameter to the file (%2) is being substituted in the above.
How do I prevent variable substitution in the above example?
Thanks in advance for your help,
John L.
-
SET I=http://www.someURL.com/My%%%20Webpage%%%28Version%%%20B%%%29.html
you have to use the escape the %, otherwise it thinks you are referring to a veritable
for most charictors (>, |, &) you need to use ^ but with % you must use another % or surround it with %'s
-
you have to use the escape the %, otherwise it thinks you are referring to a veritable
for most charictors (>, |, &) you need to use ^ but with % you must use another %
That's correct.
or surround it with %'s
That bit is wrong. Just double the percent signs.
SET I=http://www.someURL.com/My%%20Webpage%%28Version%%20B%%29.html
-
interesting, triple % has always worked for me, but maybe only in certain instances.
-
interesting, triple % has always worked for me, but maybe only in certain instances.
What circumstances?
-
What circumstances?
Everything I've ever written, would you like me to post some examples? I'll have to go digging.
-
would you like me to post some examples?
Yes please.
-
Here, I'll do it for you.
@echo off
echo Passed parameters:
echo Parameter 1 %1
echo Parameter 2 %2
echo.
echo Attempting to escape percent signs in string:
echo.
echo 1. No escaping
SET I=http://www.someURL.com/My%20Webpage%28Version%20B%29.html
echo %I%
echo.
echo 2. Replace one percent sign with two
SET I=http://www.someURL.com/My%%20Webpage%%28Version%%20B%%29.html
echo %I%
echo.
echo 3. Replace one percent sign with three
SET I=http://www.someURL.com/My%%%20Webpage%%%28Version%%%20B%%%29.html
echo %I%
echo.
echo 4. Replace one percent sign with four
SET I=http://www.someURL.com/My%%%%20Webpage%%%%28Version%%%%20B%%%%29.html
echo %I%
echo.
1. With parameters passed
Passed parameters:
Parameter 1 cat
Parameter 2 dog
Attempting to escape percent signs in string:
1. No escaping
http://www.someURL.com/Mydog0Webpagedog8Versiondog0Bdog9.html
2. Replace one percent sign with two
http://www.someURL.com/My%20Webpage%28Version%20B%29.html
3. Replace one percent sign with three
http://www.someURL.com/My%dog0Webpage%dog8Version%dog0B%dog9.html
4. Replace one percent sign with four
http://www.someURL.com/My%%20Webpage%%28Version%%20B%%29.html
With no parameters passed
Passed parameters:
Parameter 1
Parameter 2
Attempting to escape percent signs in string:
1. No escaping
http://www.someURL.com/My0Webpage8Version0B9.html
2. Replace one percent sign with two
http://www.someURL.com/My%20Webpage%28Version%20B%29.html
3. Replace one percent sign with three
http://www.someURL.com/My%0Webpage%8Version%0B%9.html
4. Replace one percent sign with four
http://www.someURL.com/My%%20Webpage%%28Version%%20B%%29.html
If you study the above you will see that to get ONE percent sign in the URL string using SET, you need exactly TWO percent signs in the code.
-
Put simply, 2N percents in the code are replaced by N percents in the displayed result, and 2N+1 percents are replaced by N+1 percents.