Escaping quotes and spaces in command line parameters

Archived from groups: microsoft.public.win2000.general (More info?)

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
1 answer Last reply
More about escaping quotes spaces command line parameters
  1. Archived from groups: microsoft.public.win2000.general (More info?)

    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 -

    On Fri, 15 Jul 2005 10:40:33 +0530, "Maansi Sanghi" <maansisanghi@abosoftware.com> wrote:

    >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
    >
    >
    >
    >
    >
    >
    >
    >
Ask a new question

Read More

Command Line Microsoft Command Prompt Windows