We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Batch file escape characters

Robmonster
Robmonster asked
on
Medium Priority
4,687 Views
Last Modified: 2012-05-07
Hi there,

Our application runs a Windows batch file passing in a string argument. This string is derived from customer data, and so it can contain quotes, ampersands, percentages and so on.

What is the best way of escaping these special characters s the data gets passed into the batch file correctly?

For example, we may call a batchfile 'lookup.bat' that accepts a Product Code as an argument. We recently had trouble with product codes containing ampersands so we changed it from running 'lookup ABC&123" to 'lookup "ABC&123"' That works fine. However, product codes legitimately containing quotes now cause a problem.

Can anyone offer any advice? We need to be able to handle quotes,ampersands and spaces in the same string.

For reference, the contents of this batchfile do something akin to the following:-

run lookup-program :"%1":  

This format is required due to the programming language involved

Rob
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
You will have to escape any of this characters by prefixing a caret::
()&%!"|

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
I foirgot to add. If you run the program in my previous post like this:

   BATCHFILE "abc&123"

It will output the following code:

   :abc&123:

Using redirection, this would form the command tails of your LOOKUP program.

Commented:
Oh, BTW, what's the name of your application and what version is it?

Commented:
Thank you for asking an interesting question.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.