escape double quotes in a variable when using echo|set /p=











up vote
1
down vote

favorite
1












echo|set /p= can output a variable without a newline.



I am trying to loop a text file like this



for /f %%a in (zen.txt) do (
set var=%%a
echo !var!
echo|set /p=!var!
)


There are some lines with only one ", for example:



"he said...


echo outputs the line like above correctly while echo|set /p= output nothing.



Is it possible to escape double quotes in a variable when using echo|set /p=.










share|improve this question







New contributor




小川流れ者 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    1
    down vote

    favorite
    1












    echo|set /p= can output a variable without a newline.



    I am trying to loop a text file like this



    for /f %%a in (zen.txt) do (
    set var=%%a
    echo !var!
    echo|set /p=!var!
    )


    There are some lines with only one ", for example:



    "he said...


    echo outputs the line like above correctly while echo|set /p= output nothing.



    Is it possible to escape double quotes in a variable when using echo|set /p=.










    share|improve this question







    New contributor




    小川流れ者 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      echo|set /p= can output a variable without a newline.



      I am trying to loop a text file like this



      for /f %%a in (zen.txt) do (
      set var=%%a
      echo !var!
      echo|set /p=!var!
      )


      There are some lines with only one ", for example:



      "he said...


      echo outputs the line like above correctly while echo|set /p= output nothing.



      Is it possible to escape double quotes in a variable when using echo|set /p=.










      share|improve this question







      New contributor




      小川流れ者 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      echo|set /p= can output a variable without a newline.



      I am trying to loop a text file like this



      for /f %%a in (zen.txt) do (
      set var=%%a
      echo !var!
      echo|set /p=!var!
      )


      There are some lines with only one ", for example:



      "he said...


      echo outputs the line like above correctly while echo|set /p= output nothing.



      Is it possible to escape double quotes in a variable when using echo|set /p=.







      windows batch-file echo double-quotes






      share|improve this question







      New contributor




      小川流れ者 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      小川流れ者 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      小川流れ者 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      小川流れ者

      283




      283




      New contributor




      小川流れ者 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      小川流れ者 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      小川流れ者 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote













          We will need to provide set/p with additional quotes to consume. You can try with something like (without the test file creation, of course)



          @echo off
          setlocal enableextensions disabledelayedexpansion

          rem TESTING - Create a input file
          >zen.txt (
          echo "He said...
          echo It was not me!
          echo It was "the thing"!
          echo ...
          echo ^& it was all" ( The end )
          )

          for /f "delims=" %%a in (zen.txt) do (
          <nul set /p="%%a "
          )


          echo|set /p simulates a return key press to terminate the set/p prompt, but it creates separate cmd instances for each side of the pipe making it slow. A <nul input redirection is enough to get the same result but faster and with less resources usage.






          share|improve this answer






























            up vote
            3
            down vote













            set /p is a bit nasty with handling whitespaces, quotes and equal signs.



            A quote at the beginning or the end has to be doubled, BUT when you want to display quotes, the expression should be enclosed in quotes, too.



            To display a single quote use



            <nul set /p ="""


            set /p seems to strip one time the outer quotes.



            Your code can be changed to <NUL set /p="!var!" that should work with normal text and also with quotes.



            If the text starts with whitespaces, they will be dropped. (But not up to XP, there set /p "= Hello" shows the spaces).



            set /p seems to use two times a quote remover.
            First for the extended set syntax
            <nul set /p "=hello" Text after the last quote will be dropped



            But also for the content, the outer quotes will be dropped
            <nul set /p ="hello" Text after the last quote will be dropped



            And even combining both works
            <nul set /p "="hello" Text after the last inner quote will be dropped "



            Note: I use <nul set /p, it's much faster, because the echo | set /p version uses a pipe and spawns two new cmd.exe instances. (Already mentioned by MC ND)






            share|improve this answer























            • +1, When I could I will take a look on how it is handled, but I think the "two time quote remover" involves the cmd parser followed by the set/p parser.
              – MC ND
              2 days ago










            • @MCND I don't think it's the cmd parser, as the quote handling should all be done only by the set command itself
              – jeb
              2 days ago






            • 1




              Checked and you are right. cmd parser passes everything to the set/p parser. The set /p parser removes the quotes in several steps. While locating variable name initial and tailing (if initial present) quotes are removed. While locating prompt literal same operation is done.
              – MC ND
              2 days ago






            • 1




              And by remove (in both cases) I mean move starting pointer for initial quote, replace last quote with 0x00, the reason why in set /p "=hello" text the text literal dissapears.
              – MC ND
              2 days ago











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });






            小川流れ者 is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53184045%2fescape-double-quotes-in-a-variable-when-using-echoset-p%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote













            We will need to provide set/p with additional quotes to consume. You can try with something like (without the test file creation, of course)



            @echo off
            setlocal enableextensions disabledelayedexpansion

            rem TESTING - Create a input file
            >zen.txt (
            echo "He said...
            echo It was not me!
            echo It was "the thing"!
            echo ...
            echo ^& it was all" ( The end )
            )

            for /f "delims=" %%a in (zen.txt) do (
            <nul set /p="%%a "
            )


            echo|set /p simulates a return key press to terminate the set/p prompt, but it creates separate cmd instances for each side of the pipe making it slow. A <nul input redirection is enough to get the same result but faster and with less resources usage.






            share|improve this answer



























              up vote
              3
              down vote













              We will need to provide set/p with additional quotes to consume. You can try with something like (without the test file creation, of course)



              @echo off
              setlocal enableextensions disabledelayedexpansion

              rem TESTING - Create a input file
              >zen.txt (
              echo "He said...
              echo It was not me!
              echo It was "the thing"!
              echo ...
              echo ^& it was all" ( The end )
              )

              for /f "delims=" %%a in (zen.txt) do (
              <nul set /p="%%a "
              )


              echo|set /p simulates a return key press to terminate the set/p prompt, but it creates separate cmd instances for each side of the pipe making it slow. A <nul input redirection is enough to get the same result but faster and with less resources usage.






              share|improve this answer

























                up vote
                3
                down vote










                up vote
                3
                down vote









                We will need to provide set/p with additional quotes to consume. You can try with something like (without the test file creation, of course)



                @echo off
                setlocal enableextensions disabledelayedexpansion

                rem TESTING - Create a input file
                >zen.txt (
                echo "He said...
                echo It was not me!
                echo It was "the thing"!
                echo ...
                echo ^& it was all" ( The end )
                )

                for /f "delims=" %%a in (zen.txt) do (
                <nul set /p="%%a "
                )


                echo|set /p simulates a return key press to terminate the set/p prompt, but it creates separate cmd instances for each side of the pipe making it slow. A <nul input redirection is enough to get the same result but faster and with less resources usage.






                share|improve this answer














                We will need to provide set/p with additional quotes to consume. You can try with something like (without the test file creation, of course)



                @echo off
                setlocal enableextensions disabledelayedexpansion

                rem TESTING - Create a input file
                >zen.txt (
                echo "He said...
                echo It was not me!
                echo It was "the thing"!
                echo ...
                echo ^& it was all" ( The end )
                )

                for /f "delims=" %%a in (zen.txt) do (
                <nul set /p="%%a "
                )


                echo|set /p simulates a return key press to terminate the set/p prompt, but it creates separate cmd instances for each side of the pipe making it slow. A <nul input redirection is enough to get the same result but faster and with less resources usage.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 2 days ago

























                answered 2 days ago









                MC ND

                57.6k54676




                57.6k54676
























                    up vote
                    3
                    down vote













                    set /p is a bit nasty with handling whitespaces, quotes and equal signs.



                    A quote at the beginning or the end has to be doubled, BUT when you want to display quotes, the expression should be enclosed in quotes, too.



                    To display a single quote use



                    <nul set /p ="""


                    set /p seems to strip one time the outer quotes.



                    Your code can be changed to <NUL set /p="!var!" that should work with normal text and also with quotes.



                    If the text starts with whitespaces, they will be dropped. (But not up to XP, there set /p "= Hello" shows the spaces).



                    set /p seems to use two times a quote remover.
                    First for the extended set syntax
                    <nul set /p "=hello" Text after the last quote will be dropped



                    But also for the content, the outer quotes will be dropped
                    <nul set /p ="hello" Text after the last quote will be dropped



                    And even combining both works
                    <nul set /p "="hello" Text after the last inner quote will be dropped "



                    Note: I use <nul set /p, it's much faster, because the echo | set /p version uses a pipe and spawns two new cmd.exe instances. (Already mentioned by MC ND)






                    share|improve this answer























                    • +1, When I could I will take a look on how it is handled, but I think the "two time quote remover" involves the cmd parser followed by the set/p parser.
                      – MC ND
                      2 days ago










                    • @MCND I don't think it's the cmd parser, as the quote handling should all be done only by the set command itself
                      – jeb
                      2 days ago






                    • 1




                      Checked and you are right. cmd parser passes everything to the set/p parser. The set /p parser removes the quotes in several steps. While locating variable name initial and tailing (if initial present) quotes are removed. While locating prompt literal same operation is done.
                      – MC ND
                      2 days ago






                    • 1




                      And by remove (in both cases) I mean move starting pointer for initial quote, replace last quote with 0x00, the reason why in set /p "=hello" text the text literal dissapears.
                      – MC ND
                      2 days ago















                    up vote
                    3
                    down vote













                    set /p is a bit nasty with handling whitespaces, quotes and equal signs.



                    A quote at the beginning or the end has to be doubled, BUT when you want to display quotes, the expression should be enclosed in quotes, too.



                    To display a single quote use



                    <nul set /p ="""


                    set /p seems to strip one time the outer quotes.



                    Your code can be changed to <NUL set /p="!var!" that should work with normal text and also with quotes.



                    If the text starts with whitespaces, they will be dropped. (But not up to XP, there set /p "= Hello" shows the spaces).



                    set /p seems to use two times a quote remover.
                    First for the extended set syntax
                    <nul set /p "=hello" Text after the last quote will be dropped



                    But also for the content, the outer quotes will be dropped
                    <nul set /p ="hello" Text after the last quote will be dropped



                    And even combining both works
                    <nul set /p "="hello" Text after the last inner quote will be dropped "



                    Note: I use <nul set /p, it's much faster, because the echo | set /p version uses a pipe and spawns two new cmd.exe instances. (Already mentioned by MC ND)






                    share|improve this answer























                    • +1, When I could I will take a look on how it is handled, but I think the "two time quote remover" involves the cmd parser followed by the set/p parser.
                      – MC ND
                      2 days ago










                    • @MCND I don't think it's the cmd parser, as the quote handling should all be done only by the set command itself
                      – jeb
                      2 days ago






                    • 1




                      Checked and you are right. cmd parser passes everything to the set/p parser. The set /p parser removes the quotes in several steps. While locating variable name initial and tailing (if initial present) quotes are removed. While locating prompt literal same operation is done.
                      – MC ND
                      2 days ago






                    • 1




                      And by remove (in both cases) I mean move starting pointer for initial quote, replace last quote with 0x00, the reason why in set /p "=hello" text the text literal dissapears.
                      – MC ND
                      2 days ago













                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    set /p is a bit nasty with handling whitespaces, quotes and equal signs.



                    A quote at the beginning or the end has to be doubled, BUT when you want to display quotes, the expression should be enclosed in quotes, too.



                    To display a single quote use



                    <nul set /p ="""


                    set /p seems to strip one time the outer quotes.



                    Your code can be changed to <NUL set /p="!var!" that should work with normal text and also with quotes.



                    If the text starts with whitespaces, they will be dropped. (But not up to XP, there set /p "= Hello" shows the spaces).



                    set /p seems to use two times a quote remover.
                    First for the extended set syntax
                    <nul set /p "=hello" Text after the last quote will be dropped



                    But also for the content, the outer quotes will be dropped
                    <nul set /p ="hello" Text after the last quote will be dropped



                    And even combining both works
                    <nul set /p "="hello" Text after the last inner quote will be dropped "



                    Note: I use <nul set /p, it's much faster, because the echo | set /p version uses a pipe and spawns two new cmd.exe instances. (Already mentioned by MC ND)






                    share|improve this answer














                    set /p is a bit nasty with handling whitespaces, quotes and equal signs.



                    A quote at the beginning or the end has to be doubled, BUT when you want to display quotes, the expression should be enclosed in quotes, too.



                    To display a single quote use



                    <nul set /p ="""


                    set /p seems to strip one time the outer quotes.



                    Your code can be changed to <NUL set /p="!var!" that should work with normal text and also with quotes.



                    If the text starts with whitespaces, they will be dropped. (But not up to XP, there set /p "= Hello" shows the spaces).



                    set /p seems to use two times a quote remover.
                    First for the extended set syntax
                    <nul set /p "=hello" Text after the last quote will be dropped



                    But also for the content, the outer quotes will be dropped
                    <nul set /p ="hello" Text after the last quote will be dropped



                    And even combining both works
                    <nul set /p "="hello" Text after the last inner quote will be dropped "



                    Note: I use <nul set /p, it's much faster, because the echo | set /p version uses a pipe and spawns two new cmd.exe instances. (Already mentioned by MC ND)







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 2 days ago

























                    answered 2 days ago









                    jeb

                    56.8k13128167




                    56.8k13128167












                    • +1, When I could I will take a look on how it is handled, but I think the "two time quote remover" involves the cmd parser followed by the set/p parser.
                      – MC ND
                      2 days ago










                    • @MCND I don't think it's the cmd parser, as the quote handling should all be done only by the set command itself
                      – jeb
                      2 days ago






                    • 1




                      Checked and you are right. cmd parser passes everything to the set/p parser. The set /p parser removes the quotes in several steps. While locating variable name initial and tailing (if initial present) quotes are removed. While locating prompt literal same operation is done.
                      – MC ND
                      2 days ago






                    • 1




                      And by remove (in both cases) I mean move starting pointer for initial quote, replace last quote with 0x00, the reason why in set /p "=hello" text the text literal dissapears.
                      – MC ND
                      2 days ago


















                    • +1, When I could I will take a look on how it is handled, but I think the "two time quote remover" involves the cmd parser followed by the set/p parser.
                      – MC ND
                      2 days ago










                    • @MCND I don't think it's the cmd parser, as the quote handling should all be done only by the set command itself
                      – jeb
                      2 days ago






                    • 1




                      Checked and you are right. cmd parser passes everything to the set/p parser. The set /p parser removes the quotes in several steps. While locating variable name initial and tailing (if initial present) quotes are removed. While locating prompt literal same operation is done.
                      – MC ND
                      2 days ago






                    • 1




                      And by remove (in both cases) I mean move starting pointer for initial quote, replace last quote with 0x00, the reason why in set /p "=hello" text the text literal dissapears.
                      – MC ND
                      2 days ago
















                    +1, When I could I will take a look on how it is handled, but I think the "two time quote remover" involves the cmd parser followed by the set/p parser.
                    – MC ND
                    2 days ago




                    +1, When I could I will take a look on how it is handled, but I think the "two time quote remover" involves the cmd parser followed by the set/p parser.
                    – MC ND
                    2 days ago












                    @MCND I don't think it's the cmd parser, as the quote handling should all be done only by the set command itself
                    – jeb
                    2 days ago




                    @MCND I don't think it's the cmd parser, as the quote handling should all be done only by the set command itself
                    – jeb
                    2 days ago




                    1




                    1




                    Checked and you are right. cmd parser passes everything to the set/p parser. The set /p parser removes the quotes in several steps. While locating variable name initial and tailing (if initial present) quotes are removed. While locating prompt literal same operation is done.
                    – MC ND
                    2 days ago




                    Checked and you are right. cmd parser passes everything to the set/p parser. The set /p parser removes the quotes in several steps. While locating variable name initial and tailing (if initial present) quotes are removed. While locating prompt literal same operation is done.
                    – MC ND
                    2 days ago




                    1




                    1




                    And by remove (in both cases) I mean move starting pointer for initial quote, replace last quote with 0x00, the reason why in set /p "=hello" text the text literal dissapears.
                    – MC ND
                    2 days ago




                    And by remove (in both cases) I mean move starting pointer for initial quote, replace last quote with 0x00, the reason why in set /p "=hello" text the text literal dissapears.
                    – MC ND
                    2 days ago










                    小川流れ者 is a new contributor. Be nice, and check out our Code of Conduct.










                     

                    draft saved


                    draft discarded


















                    小川流れ者 is a new contributor. Be nice, and check out our Code of Conduct.













                    小川流れ者 is a new contributor. Be nice, and check out our Code of Conduct.












                    小川流れ者 is a new contributor. Be nice, and check out our Code of Conduct.















                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53184045%2fescape-double-quotes-in-a-variable-when-using-echoset-p%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    Popular posts from this blog

                    横浜市

                    Rostock

                    Europa