You are not logged in.

#1 28 Sep 2020 09:26

PiotrMP006
Member
Registered: 02 Apr 2019
Posts: 12

How to remove Quotes from a variable with "ampersand" (&) ?

How to remove Quotes from a variable with "ampersand" (&) ?

ex

setlocal disabledelayedexpansion

set dir="D:\Test ! & Test\"

set dir=%dir:"=%

after

echo %dir%

D:\Test !  Test\

Sign & is missing !!!!

Please help

Offline

#2 28 Sep 2020 11:12

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: How to remove Quotes from a variable with "ampersand" (&) ?

I don't see any problem with your code to remove the quotes for files (seems to do the job for me).  But you would need to be careful with how you handle the resulting string -- a straight `echo` like you have done is not likely to work, as the variable will be expanded and the `&` will try to operate as a command delimiter. 

This works for me:

set dir="D:\Test ! & Test\"
REM NB - while it looks a little odd, it's good coding practice to
REM      always quote wrap your `set` commands like this
set "dir=%dir:"=%"

<nul set /p "=%dir%" & echo:

This slight misuse of `set` allows you to quote outputting a string without them becoming embedded.  For instance this would also work:

echo "%dir%"

... but the quotes would appear in the resulting output, which one usually wouldn't want to happen.

EDIT: A side effect (and often a useful effect) of outputting via `set /p` is that the output will not have a new line on the end.
The `& echo:` is just tacking on the missing new line to mimic normal use of `echo:somevalue`.

Last edited by bluesxman (07 Oct 2020 17:13)


cmd | *sh | ruby | chef

Offline

#3 05 Oct 2020 15:14

Try3
Member
Registered: 20 Jul 2019
Posts: 9

Re: How to remove Quotes from a variable with "ampersand" (&) ?

bluesxman,

That's a very interesting technique.  Thanks very much for posting it.

I don't understand what the echo is achieving.  I can see that it puts an extra line break in the cmd window but wondered if it is doing more than that.

I played around with your code and found that it enabled me to write the variable to an output file without quotes getting in the way

set "TestVar=D:\Test ! & Test\"
<nul set /p "=%TestVar%">TestOutput.txt 

and I think this will come in handy.

Denis

Offline

#4 05 Oct 2020 15:55

Try3
Member
Registered: 20 Jul 2019
Posts: 9

Re: How to remove Quotes from a variable with "ampersand" (&) ?

bluesxman,

That's a very interesting technique.  Thanks very much for posting it.

I played around with your code and found that it enabled me to write the variable to an output file without quotes getting written along with the variable.

set "TestVar=D:\Test ! & Test\"
<nul set /p "=%TestVar%">TestOutput.txt
echo:>>TestOutput.txt

The echo is needed to stop the next line sent to the output file being written on the same line as the variable.

and I think this will come in handy.

Denis

Offline

#5 07 Oct 2020 17:10

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: How to remove Quotes from a variable with "ampersand" (&) ?

Editing my answer to include information about the extra `& echo:`

Last edited by bluesxman (07 Oct 2020 17:13)


cmd | *sh | ruby | chef

Offline

#6 15 Oct 2020 14:27

tra
Member
From: Andorra
Registered: 15 Oct 2020
Posts: 6

Re: How to remove Quotes from a variable with "ampersand" (&) ?

you can try this:

setlocal enableextensions
set dir="D:\Test ! & Test\"
echo !dir:"=!

if you use the ! inplace of %, cmd will process environment var like a string


cmd|ps|rexx|ada|C|C++|C#|vba|java|js|rpg|cobol|mih|perl|al

Offline

#7 15 Oct 2020 21:18

Try3
Member
Registered: 20 Jul 2019
Posts: 9

Re: How to remove Quotes from a variable with "ampersand" (&) ?

tra,

OK but, unlike Bluesman's method, yours cannot be extended to write the variable to a file.

No output file is created by

setlocal enableextensions
set dir="D:\Test ! & Test\"
echo !dir:"=! >TestOP.txt
pause

Denis

Offline

#8 15 Oct 2020 23:38

tra
Member
From: Andorra
Registered: 15 Oct 2020
Posts: 6

Re: How to remove Quotes from a variable with "ampersand" (&) ?

you're right, to solve this problem, the script are to be something like this:

setlocal enabledelayedexpansion
set dir="D:\Test ^! & Test\"
set dir=!dir:"=!
echo !dir!>TestOP.txt
pause

symbol ! has to be escaped with ^! and echo only is able to process like a string when use envvar directly without substitution pattern


cmd|ps|rexx|ada|C|C++|C#|vba|java|js|rpg|cobol|mih|perl|al

Offline

#9 16 Oct 2020 00:14

Try3
Member
Registered: 20 Jul 2019
Posts: 9

Re: How to remove Quotes from a variable with "ampersand" (&) ?

tra,

Thanks for responding but I think I'll have to stick with Bluesman's method.

I cannot insert ^ into the set command line because, in real cases, the variable might be the output from a command over which I do not have the level of control needed - such as a For iteration returning file paths-names.

I was not able to alter your suggested code to avoid the ^ yet preserve the ! in a variable value.  I tried several variations of your suggested code but the ! did not make it through [but does in Bluesman's method].

All the best,
Denis

Offline

Board footer

Powered by