Escaping quotes and spaces in command line parameters

  • Thread starter Thread starter Maansi Sanghi
  • Start date Start date
M

Maansi Sanghi

Hello,

(1) I have created a batch file "test.bat" which takes a parameter from
command line and writes it to a file. The Batch has just one command which
is mentioned below.

echo %1 >>Test.txt

(2) The problem is that the input paramater can be a string which can have
both spaces and quotes within it.
Problem arrises with command line strings which have BOTH quotes and spaces

a) A Space can be handled by surrounding Quotes
test.bat "TESTSPACE ONLY"

b) When a String contains a Quote
test.bat "TESTQUOTE" ONLY"
There is an error in the above command

c) When there is a String and Quote both
test.bat "TESTQUOTE" And Space"
It only takes the part TestQuote


The escape chatacter ^ also does not work over here

My operating system is Windows Professional. Is it possible to accept
strings which may contains both quotes and spaces as paramateres??

Regards,
Maansi
 
Use the following test8.bat as an example:

@echo off
@echo %*
@echo 1 - %1
@echo 2 - %2
@echo 3 - %3
@echo 4 - %4

test8 "The little brown fox."
"The little brown fox."
1 - "The little brown fox."
2 -
3 -
4 -


test8 The little brown fox.
The little brown fox.
1 - The
2 - little
3 - brown
4 - fox.


test8 The "little" brown fox.
The "little" brown fox.
1 - The
2 - "little"
3 - brown
4 - fox.

test8 "The ""little"" brown fox."
"The ""little"" brown fox."
1 - "The ""little"" brown fox."
2 -
3 -
4 -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top