Computer Hope

Microsoft => Microsoft DOS => Topic started by: John_L. on September 04, 2012, 02:20:51 PM

Title: How to prevent variable substitution in SET command
Post by: John_L. on September 04, 2012, 02:20:51 PM
I am trying to do the following:

Code: [Select]
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.
Title: Re: How to prevent variable substitution in SET command
Post by: Lemonilla on September 04, 2012, 03:11:43 PM
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
Title: Re: How to prevent variable substitution in SET command
Post by: foxidrive on September 04, 2012, 09:39:26 PM
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.


Quote from:
or surround it with %'s

That bit is wrong.   Just double the percent signs.


Code: [Select]
SET I=http://www.someURL.com/My%%20Webpage%%28Version%%20B%%29.html
Title: Re: How to prevent variable substitution in SET command
Post by: Lemonilla on September 05, 2012, 02:32:04 PM
interesting, triple % has always worked for me, but maybe only in certain instances.
Title: Re: How to prevent variable substitution in SET command
Post by: Salmon Trout on September 06, 2012, 01:43:23 PM
interesting, triple % has always worked for me, but maybe only in certain instances.

What circumstances?

Title: Re: How to prevent variable substitution in SET command
Post by: Lemonilla on September 07, 2012, 01:51:18 PM
What circumstances?


Everything I've ever written, would you like me to post some examples? I'll have to go digging.
Title: Re: How to prevent variable substitution in SET command
Post by: Salmon Trout on September 07, 2012, 02:13:10 PM
would you like me to post some examples?

Yes please.
Title: Re: How to prevent variable substitution in SET command
Post by: Salmon Trout on September 08, 2012, 02:19:14 AM
Here, I'll do it for you.

Code: [Select]
@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

Code: [Select]
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

Code: [Select]
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.



Title: Re: How to prevent variable substitution in SET command
Post by: Salmon Trout on September 08, 2012, 04:06:11 AM
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.