Solved

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

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now