Solved

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

Posted on 2014-09-08
3
297 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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