Problem with quoted filenames in Batch - windows

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?

Related

How to escape % inside double quotes in cmd?

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%%

Windows bacth scripting: Problem exporting variable to txt

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>"

& sign inside batch file parameter to be passed for another program

I have a batch file that I call with something like this
call do.cmd "one two"
In do.cmd I am launching a program and pass to it first parameter from above:
#echo off
some_program.exe -name='%1'
The value to some_program.exe for name variable must be passed inside single quotes without surrounding double quotes. To get rid of double quotes in passed parameter I make a temporary variable like this:
set v_tmp=%1
set v_tmp=%v_artist:"=%
And then launch my program by
some_programm.exe -name='%v_tmp%'
The problem start when with do.cmd some text having & sign is passed. If I leave batch file code as is, variaable setting will fail because & will act as a divider. If I escape & sign by
set v_tmp=%1
set v_tmp=%v_tmp:&"=^^^&%
set v_tmp=%v_tmp:"=%
then some_program will output text having ^&..
The question is how do I get from call do.cmd "one & two" line to the correct & sign escaping and double quote removal so that to have in result some_program.exe -name='one & two'?
You could try to use delayed expansion here.
#echo off
set "arg1=%~1"
setlocal EnableDelayedExpansion
some_program.exe -name='!arg1!'
This works, as %~1 removes enclosing quotes, if present.
And !arg1! always expands the variable in a safe manner.

How Do You Show All Characters In Echo On In Batch

I'm having a problem where I can't get all characters (especially special characters) to show when I try to echo them
echo 1234567890q!w#e#r$t%y^u&io()pa;s/d.f,ghjklzxcvb nmQAZXSWEDCVFRTGBNHYUJMKIOLP
When using this code in batch, it doesn't show the characters (especially the special characters) but instead say "This program is not recognizable as an internal or external command, operable program or batch file.". The reason to why this is saying this is because the lines have special characters but how do I show the special characters in echo on? Thanks people.
Neither of those lines results in that particular error message when I test them from the command line (although the first results in "Environment variable 123456etc. not defined"). You have properly quoted your "variable=value" pair, and there is no problem setting CHARSET to that value.
I suspect the problem is that when you echo %charset% the & is being evaluated as a command separator, and evaluating the string thereafter as a new command. Try retrieving the value of CHARSET in the delayed expansion style.
set "charset=1234567890q!w#e#r$t%%y^u&io()pa;s/d.f,ghjklzxcvb nmQAZXSWEDCVFRTGBNHYUJMKIOLP"
setlocal enabledelayedexpansion
echo !charset!
endlocal
By the way, if you want to include a literal percent, you should do it double (%%).

Batch path variable with space - how to escape the double quotes?

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?

Resources