Solved

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

Posted on 2014-09-08
3
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 70

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

622 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