I'm setting a variable with this directory
C:\My dir\Sub dir\Folder with a lot of spaces\file.html
like this:
SET location="C:\My dir\Sub dir\Folder with a lot of spaces\file.html"
The problem is, when I echo %location% it comes with both double quotes. I've tried to remove using this:
set location=%location:"=\"%
But it does not work. I've searched a lot and I can't find an easy and simple solution. I'm using Windows 8.
An easy and simple solution is to store the path without the quotes, like this:
SET "location=C:\My dir\Sub dir\Folder with a lot of spaces\file.html"
This way the value will be stored with no quotes and the SET statement will still be protected from the effect of certain special characters. You will then either enclose %location% in quotes or omit the quotes as required. For instance, if you are using the value as an argument of some command, you will likely need to put the quotes around it:
somecommand "%location%" …
…
And if you just need to output the path, the quotes may be unnecessary – and it's easy to leave them out:
ECHO Location: %location%
…
Note that if the path has characters with special meaning, like &, ECHOing the path without the quotes would cause such characters to be interpreted in % evaluation. To avoid that, you could temporarily enable and use delayed expansion:
SETLOCAL EnableDelayedExpansion
ECHO Location: !location!
ENDLOCAL
…
Use set "location=C:\My dir\Sub dir\Folder with a lot of spaces\file.html" to keep the spaces but leave out the double quotes.
Don't use quotes in Set.
Later when you use that variable you include quotes around it eg
type "%location%"
Also this removes quotes
set location=%location:"=%
Yours I think replaces quotes with a backslash and a quote, not very useful.
Why even put it into a variable?
Related
In cmd I'm trying to do something like
program.exe -command "otherprogram.exe %thing% %path%"
The issue I'm having is that I can't figure out how to escape the % characters when they're inside double quotes, but I need the double quotes because of the spaces in this argument. Basically I don't want cmd to do variable expansion before passing the argument value to program.exe.
Just to be clear, this is directly in cmd, not in a batch script.
A simple semi solution is:
program.exe -command ^"otherprogram.exe %th^ing% %pa^t^h%^"
The positions of the carets inside the variable name are random.
This still could fail, but only for the rare case, if variables exists named thi^ng or pa^t^h
It seems strange, but don't escape the percent signs. Put a caret (the escape sign for every other special char) anywhere within the variable name: echo %^username% or `echo %use^rname%.
The first parsing removes the ^ (because there is (hopefully) no variable with that name). On command line (other than in a batch file), an empty variable doesn't show nothing, but the variable name including the surrounding %'s.
Any further level of parsing then receives the "normal" variable and expands it. Prove:
echo %usern^ame%
call echo %usern^ame%
Not your question, but for the sake of completeness: in a batch script, simply escape the % with another %:
echo %%username%%
call echo %%username%%
I have a variable with html code (having major, minor symbols)
and I need it to be exported and appended to a txt
set WORD1=^<p^>^<strong^>PROBLEM^</strong^> with something;n^</p^>
I can't echo the variable like this
echo %WORD1%
And I need to export it/append it to a file. I used:
echo %WORD1% >body.txt
But this generates an error as the variable has minor/Major symbol
If I double quote the variable, the exported text is exported with double quotes (and obviusly this is not what i need)
To define the variable, use:
set "WORD1=<p><strong>PROBLEM</strong> with something;n</p>"
To "export" the variable, use:
(
set /P "=%WORD1%"
echo/
) > body.txt < NUL
To define a variable in a safe way you need to enclose the whole assignment expression in quotation marks:
set "WORD1=<p><strong>PROBLEM</strong> with something;n</p>"
This avoids the need of escaping, unless the string itself contains quotation marks on its own.
Note that this syntax only works with command extensions enabled, but this is the default in Command Prompt anyway.
To return/expand an arbitrary string in a safe manner, even when it contains quotation marks on its own, is to use delayed variable expansion:
echo(!WORD1!
To safely write the output to a file, place the redirection expression at the front:
> "body.txt" echo(!WORD1!
You can also do this on one line:
set "WORD1=<p><strong>PROBLEM</strong> with something;n</p>"&&>body.txt cmd/v/cecho.!WORD1!
rem :: Or, without defining a previous variable, if it is not necessary:
>body.txt <nul set/P "=<p><strong>PROBLEM</strong> with something;n</p>"
Can I do something like ecranning quotes in variable? For example,
if "%1"=="/?" goto :help
Will return error if argument is "
Try "%~1". The tilde modifier will strip quotes from %1, if present.
Unfortunately there is no general safe way to process strings with special chars (quotes, parentheses, caret) in batch files. There are always some cases that blow up whatever you write.
In a Windows XP .bat file, how do I set a variable value to use double quote? I couldn't find special characters for batch file.
SET myVariable= " \"myValue \" "
You can use the method #Patrick Cuff has offered or you can do it quite simply:
SET var="Value"
Let's see if it works...
ECHO %var%
and the output is:
"Value"
Yes! :)
If this is for Windows, you need to escape the double quotes with a caret (^):
set myVariable=^"myVlaue^"
Putting single quotes around the value won't work, the value will include the single and double quotes.
You can use
SET myVariable='"myValue"'
Let I have a batch program:
SET FOO=C:\temp\%1
bar.exe %FOO%
When I call it with double quoted file name as an argument I get these quotes in the middle; and that fact prevents other programs from working correctly:
> fail.bat "aa bb.jpg"
SET FOO=C:\temp\"aa bb.jpg"
> bar.exe C:\temp\"aa bb.jpg"
cannot find file
How to get variable containing correct value "C:\temp\aa bb.jpg"?
You can use %~1 instead, this removes the quotes from the parameter.
Then your code should look like
SET FOO="C:\temp\%~1"
bar.exe %FOO%
Try removing the drive letter as I have had issues with that in the past. Also does it work if the entire path name is in quotes not just the single item with spaces?