Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a batch file with arguments having quotes

Tags:

batch-file

I have a batch file test.bat and it takes 2 arguments from commandline.

Arg1 - "C:\work Area\"
Arg2 - "Hello politics="Hero Jack""

I have test.bat calling as

test.bat "C:\work Area\" "Hello politics="Hero Jack""

I want the second argument to be taken by bat file as Hello politics="Hero Jack". I dont know how to call the test.bat with argument such that it happens so.. The second argument it takes is like "Hello politics="Hero and it discards the Jack". Can you let me know what I am calling wrongly..

like image 484
Sharma Avatar asked Nov 06 '22 05:11

Sharma


1 Answers

In my opinion, it is not possible to get it as one parameter

Arg2 - "Hello politics="Hero Jack""

It's because the paramter tokenizer is not very interested in carets. It seems, that the tokenizer count only quotes.

The parser detects carets and can escape special characters like & > or <
But the tokenizer who split the line into the %1,%2 parameters use the rule. A delimiter like , ; = starts a new parameter, this can only suppressed, if there is an unequal count of quotes before

test.bat """This is one param"
test.bat ""These are four params"
test.bat ^"This is also one param"
test.bat ^^"This is also one param"

But you can use a workaround, like

test.bat "C:\work Area\" "Hello politics=""Hero Jack""

This creates in %2 "Hello politics=""Hero Jack""
Then you can replace all double quotes with a single quote.

like image 79
jeb Avatar answered Nov 09 '22 04:11

jeb