escape double quotes in a variable when using echo|set /p=
up vote
1
down vote
favorite
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
New contributor
小川流れ者 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
1
down vote
favorite
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
New contributor
小川流れ者 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
windows batch-file echo double-quotes
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.
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.
add a comment |
add a comment |
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.
add a comment |
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)
+1, When I could I will take a look on how it is handled, but I think the "two time quote remover" involves thecmdparser followed by theset/pparser.
– 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 thesetcommand itself
– jeb
2 days ago
1
Checked and you are right.cmdparser passes everything to theset/pparser. Theset /pparser 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 with0x00, the reason why inset /p "=hello" textthetextliteral dissapears.
– MC ND
2 days ago
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited 2 days ago
answered 2 days ago
MC ND
57.6k54676
57.6k54676
add a comment |
add a comment |
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)
+1, When I could I will take a look on how it is handled, but I think the "two time quote remover" involves thecmdparser followed by theset/pparser.
– 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 thesetcommand itself
– jeb
2 days ago
1
Checked and you are right.cmdparser passes everything to theset/pparser. Theset /pparser 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 with0x00, the reason why inset /p "=hello" textthetextliteral dissapears.
– MC ND
2 days ago
add a comment |
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)
+1, When I could I will take a look on how it is handled, but I think the "two time quote remover" involves thecmdparser followed by theset/pparser.
– 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 thesetcommand itself
– jeb
2 days ago
1
Checked and you are right.cmdparser passes everything to theset/pparser. Theset /pparser 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 with0x00, the reason why inset /p "=hello" textthetextliteral dissapears.
– MC ND
2 days ago
add a comment |
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)
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)
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 thecmdparser followed by theset/pparser.
– 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 thesetcommand itself
– jeb
2 days ago
1
Checked and you are right.cmdparser passes everything to theset/pparser. Theset /pparser 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 with0x00, the reason why inset /p "=hello" textthetextliteral dissapears.
– MC ND
2 days ago
add a comment |
+1, When I could I will take a look on how it is handled, but I think the "two time quote remover" involves thecmdparser followed by theset/pparser.
– 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 thesetcommand itself
– jeb
2 days ago
1
Checked and you are right.cmdparser passes everything to theset/pparser. Theset /pparser 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 with0x00, the reason why inset /p "=hello" textthetextliteral 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
add a comment |
小川流れ者 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.
小川流れ者 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password