1

I am trying to pass an argument to a BAT file thru VB Shell function. argument text value looks like this say Hello&123 As you noticed there's an '&' as text in it. However & is reserved in BAT so I am trying to make it work but so far unsuccessful.

My VB 6 code is

Dim passwd
passwd = "Hello^&123"
retval = Shell("C:\test.bat " & passwd, vbNormalFocus)

I have tried following combinations but none work. So I am clueless what value should I set to variable passwd in VB 6 that will accept Hello&123 as correct text parameter in BAT.

Errors I receive

passwd = "Hello^&123"

 Hello
'123' is not recognized as an internal or external command,

passwd = """Hello^&123"""

"Hello^&123"

passwd = "Hello&&123"

Hello

However none of the above are useful as I want %1 to be exactly as Hello&123

Any further help will be great.

Edit...

The batch file just now simply has echo command.

@echo off
echo %1
pause

Thanks.

  • 1
    wrapping & in quotes work because it loses the special meaning inside quotes. Probably the issue is in the way you're printing the variable. What's the content of the batch file? – phuclv Feb 10 at 1:03
  • The error is probably in the batch file. You are passing the parameter correctly but then the batch gets the ampersand. You need to escape it again for the batch file. Perhaps ^^^&. – Mark Feb 10 at 1:18
2

The error is probably in the batch file. You are passing the parameter correctly but then the batch gets the ampersand. You need to escape it again for the batch file. Perhaps ^^^&.

^ is the quote character so ^^ = ^.

So to cmd gets ^^^& and changes ^^ to ^ and ^& to & so the end result is ^&. When CMD reads your batch file it changes ^& to &. So CMD needs to read this twice.

Quotes are often easier if typing at the prompt.

| improve this answer | |
3

Not the input should be changed, but the handling should.

test.bat Hello^&123
... or with quotes ...
test.bat "Hello&123"

@echo off

set "arg1=%~1"
setlocal EnableDelayedExpansion

echo arg1=!arg1!

Explanation

  1. %~1 strip quotes, when the argument is enclosed.
  2. set "arg=..." is the extended SET syntax, it enclosed the content into quotes, but doesn't add the quotes to the variable. `
  3. echo ...!arg1!` used the delayed expansion, as delayed expansion can handle any content from variables without interpreting it.
| improve this answer | |
1

I am just using Answer space to show finally what worked. User Mark is right.

Dim passwd
passwd = "Hello^^^&123"
retval = Shell("C:\test.bat " & passwd, vbNormalFocus)

and the BAT file right now simply has

@echo off
echo %1
pause

See the result now. To keep it simple I just invoking test.bat at command prompt.

enter image description here

| improve this answer | |
  • 1
    You're using echo %1 exactly as I guessed. The argument still contains the string Hello^&123 as expected but when echoing the string ^ is a normal escape character so obviously it won't work – phuclv Feb 10 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.