Solved

Batch file: how to call <label> with parameter with & inside?

Posted on 2014-09-08
3
302 Views
Last Modified: 2014-09-08
I have this batch:
set p=abcde
call :mysub %p%
exit /b
:mysub
echo %1

Open in new window

and it works as expected. But if instead of abcde I have a string with & inside,
set p=ab&de
call :mysub %p%
exit /b
:mysub
echo %1

Open in new window

it does not work, as I understand because & is interpreted as escape character. I tried to put various numbers of ^ before & in line 1, but nothing works.

How to get it to work so parameter %1 gets the actual string ab&cd ?
0
Comment
Question by:Vadim Rapp
3 Comments
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40310256
Give this a try
set p=ab^&de
call :mysub %p%
exit /b
:mysub
echo %1

Open in new window

0
 
LVL 6

Assisted Solution

by:Steve Whitcher
Steve Whitcher earned 100 total points
ID: 40310258
Instead of escaping out the individual character, try wrapping the string in double quotes.  For example:

set p="ab&de"
call :mysub %p%
exit /b
:mysub
echo %1

Open in new window


Edit: Actually, I just tested and found that didn't behave as I expected, sorry.  
Instead, include the variable name in the quotes as well, like this:

set "p=ab^&de"
call :mysub %p%
exit /b
:mysub
echo %1

Open in new window

0
 
LVL 69

Accepted Solution

by:
Qlemo earned 400 total points
ID: 40310268
First error: Your set is already failing, only setting p  to ab and then trying to execute de.
Next issue: You need to add ^^ for each instance of providing the value as a string. But that is dangerous, and behaviour strange if not using the correct amount of carets.
One way to get a half-way decent result is:
set "p=ab&de"
call :mysub "%p%"
exit /b
:mysub
echo "%~1"

Open in new window

but in fact it is very difficult to correctly treat some characters in a batch file, because of the simple string replacement strategy it uses. Switch to PowerShell or VBS for such cases ;-).
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question