You are not logged in.
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
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
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
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
Editing my answer to include information about the extra `& echo:`
Last edited by bluesxman (07 Oct 2020 17:13)
cmd | *sh | ruby | chef
Offline
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
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
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
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