How Do You Show All Characters In Echo On In Batch - windows

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 (%%).

Related

How to assign a command line parameter which contains ! and & to a variable?

Please consider the following very simple batch script (the file is named test.cmd):
#echo off
set "var1=%~1"
echo %var1%
The script should be called with one command line parameter, should assign the string which is contained in that parameter to a variable, and should output the variable.
As expected, I get an error message when I call this script with a command line parameter which contains an ampersand (&):
C:\Batch>test "a&b"
a
'b' is not recognized as an internal or external command,
operable program or batch file.
The reason for this has been discussed in some other questions here and elsewhere, for example that one; the usual remedy is to use delayed expansion. So I changed the script accordingly:
#echo off
setLocal enableDelayedExpansion
set "var1=%~1"
echo !var1!
Now it works with the parameter from before:
C:\Batch>test "a&b"
a&b
But there is a new problem. When the command line parameter contains an exclamation mark (!), it will be dropped from the output:
C:\Batch>test a!b
ab
This behavior also has been discussed at several places, for example here; the crucial thing to note is that dropping the exclamation mark happens during the assignment, not during the echo.
Despite a lot of research, I did not find a question here which provided an elegant solution for both problems at once. That is, is there an elegant way to assign a command line parameter to a variable when that parameter contains an ampersand AND an exclamation mark?
It seems that I need the delayed expansion to treat the ampersand correctly, but this destroys the exclamation mark.
The only solution I currently see is to not use delayed expansion and to add code to explicitly quote all ampersands in the input string. This would be so ugly that I seriously think that I am missing something here.
As a side note, the reason for the problem actually seems to be that there (IMHO!) is no way to get the command line parameter in a delayed-expanded fashion. The syntax for the first parameter is %~1, there is no such thing as !~1.
Move the setLocal enableDelayedExpansion after the the set„ that's all.
#echo off
set "var1=%~1"
setLocal enableDelayedExpansion
echo !var1!

Windows CMD - Why Are Both Carets In Front of Exclamation Mark Removed When Delayed Expansion Is DISABLED?

I am trying to understand more about how the Windows CMD parser works. I have been reading several posts about the CMD parser, including this one, but I can't seem to figure out why both carets (^) in the following code are stripped when delayed expansion is DISABLED:
#echo off
setlocal disabledelayedexpansion
set $test_var=This is text with escaped delayed expansion syntax - ^^!$var1^^! and ^^!$var2^^!
echo $test_var = %$test_var%
echo.
pause
I expected the result of running the code to produce the following output:
$test_var = This is text with escaped delayed expansion syntax - ^!$var1^! and ^!$var2^!
Instead, ALL carets are removed:
$test_var = This is text with escaped delayed expansion syntax - !$var1! and !$var2!
From reading the post about the parser, it is my understanding that Phase 2 removes special characters, which includes the caret (^) escape character. From my reading it seems that only one (1) caret character should be removed. Why are both carets removed?
Thanks For Your Help!
The key to the answer is the fact that normal (%-)expansion (phase 1) occurs before recognition of special characters (in phase 2) — refer to the accepted answer to: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
The first carets (^) become removed by the command interpreter in the line
set $test_var=This is text with escaped delayed expansion syntax - ^^!$var1^^! and ^^!$var2^^!
when phase 2 is done, so every instance of ^^ becomes one literal ^. You can prove this when you change #echo off to #echo on, so each parsed command line becomes echoed before being executed, or when you place the following command in the next line (thanks to user jeb for the hint):
rem // This displays the actual content of the variable:
set $test_var
In your next line
echo $test_var = %$test_var%
removal of the remaining carets happens, because – as already said – %-expansion (phase 1) happens first, resulting in a command line like
echo $test_var = This is text with escaped delayed expansion syntax - ^!$var1^! and ^!$var2^!
and then phase 2 follows, where the remaining carets are recognised and removed, resulting in this final text
$test_var = This is text with escaped delayed expansion syntax - !$var1! and !$var2!
You can protect the carets (as well as any other special characters) in the set command line when using the quoted syntax, like this:
set "$test_var=This is text with escaped delayed expansion syntax - ^!$var1^! and ^!$var2^!"
So you can save one level of escaping. However, this only works when the command extensions are enabled, but this is the default setting of the command interpreter anyway.
For the echo command line however, you cannot use such a method, because quotation marks became returned too.
I spotted that you used echo. to output an empty line. You should better use echo/ or echo( (refer to the external resource ECHO. FAILS to give text or blank line - Instead use ECHO/ to find out why).
By the way, your displayed text does not match the actual situation, because you have got delayed expansion disabled.

Call .exe from batch file with variable path containing spaces

I want to launch a windows executable from a batch file where the path to the executable is stored in a variable.
#echo off
set qtpath=C:\Program Files\Qt\5.7\mingw53_32\bin
set execpath=%qtpath%\windeployqt.exe
echo %execpath%
%execpath% --someparams
Unfortunately executing my script throws an error:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
Looks like somehow the string gets terminated at the space in Program Files.
You should change your code to this:
#echo off
set "qtpath=C:\Program Files\Qt\5.7\mingw53_32\bin"
set "execpath=%qtpath%\windeployqt.exe"
echo "%execpath%"
"%execpath%" --someparams
The SPACE, like also TAB, ,, ;, =, VTAB (vertical tabulator, ASCII 0x0B), FF (form-feed, ASCII 0x0C) and NBSP (non-break space, ASCII 0xFF) constitute token separators in the command prompt cmd. To escape tokenisation enclose your path in between "". This avoids also trouble with special characters like ^, ( and ), &, <, > and |.
The quotation marks in the set command lines again avoid trouble with special characters; they do not become part of the variable value because they enclose the entire assignment expression. Note that this syntax requires the command extensions to be enabled, but this is the default anyway.
I recommend not to include the quotation marks into variable values (set VAR="some value"), because then you could run into problems particularly when concatenating strings due to unwanted (double-)quotation (for instance, echo "C:\%VAR%\file.txt" returns "C:\"some value"\file.txt").
You are perfectly right. If the path to the file you want to execute contains spaces, you have to surround it with quotation marks:
#echo off
set qtpath=C:\Program Files\Qt\5.7\mingw53_32\bin
set execpath="%qtpath%\windeployqt.exe"
echo %execpath%
%execpath% --someparams
This should work.
It will also work when you surround %execpath% with quotation marks:
"%execpath%" --someparams

Special Characters in Batch File

Special characters in batch files are a pain, but I haven't found the right workaround for properly escaping the first two characters of this particular string I'm trying to pass the application.
SET pass=^&AntiBatchfileString
A_Program.exe /pass=%pass%
Things I have tried:
:: Escaping the escape twice, first for ^, second for &.
SET pass=^^^^&AntiBatchfileString
echo %pass%
:: Combining escapes.
SET first=^^
SET second=^^&AntiBatchfileString
SET pass=%first%%second%
echo %pass%
:: Preventing expansion
SET first=^^
SET second=^^&AntiBatchfileString
SET pass=!first!%second%
echo %pass%
:: I got this to print correctly
SET "pass=^&AntiBatchfileString"
echo ^^%pass%
Still when passing the last one it doesn't accept the login, I don't know what the final output is. That got me thinking maybe it was trying to do another expansion when passing the parameter to the application, so I quoted that as well.
SET "pass=^&AntiBatchfileString"
A_Program.exe "/pass=^^%pass%"
It's still not working, I'm not sure what I'm missing at this point.
Supposing you want the string ^&AntiBatchfileString literally, this is the best set syntax, as most special characters (^ & ( ) < > | and also the standard delimiters , ; = SPACE TAB) lose their particular meaning as soon as ther are placed in between "", and the "" themselves do not become part of the variable value:
set "pass=^&AntiBatchfileString"
This works only as long as the command extensions are on, which is the Windows default anyway (type cmd /? and see the /E option).
When expanding (reading) a variable like "%pass%" (with enclosing ""), special characters are still treated literally.
However, as soon as you expand it like %pass% (no ""), they get back their special meaning. So you have the following options:
Use set "pass=^^^&AntiBatchfileString", where ^^ escapes the literal ^ and ^& the literal & when reading like %pass%.
Enable delayed expansion (see set /? about how it works and setlocal /? or cmd /? about how to enable it), where the variable value is expanded (read) at a point of time where parsing of special characters has already been completed.
I prefer the latter approach, because no special escaping is necessary, and it can also deal with " appearing in the string value (even if unsymmetrically present).
By the way, " can also be escaped by ^", as long as this does not appear within unescaped "".
Nevertheless, % signs cannot be escaped like ^% in a batch file, because percent expansion happens before escaping, but you need to double them like %% to get one literal one each, independent whether or not the string is in between "".
Note that on the console, %% does not work.
Finally, literal ! are consumed by the delayed expansion feature when enabled, therefore you need to pay particular attention to those in case, by escaping them like ^!, or also by intelligently toggling delayed expansion (hence to enable it only when it is actually needed and to disable it otherwise, when a literal string is provided, like in a set command line, for instance, when expanding a standard variable like %pass% and when reading a for variable like %%I (batch file) or %I (console), for example). Of course this is also not the ultimate solution, because you need setlocal and endlocal to enable/disable delayed expansion, which are intended to localise environment changes, so any variable changes since the most recent setlocal command are lost as soon as endlocal is executed (there are some tricks for passing a variable value over the endlocal barrier though).
If you want to use % as a string without escaping in a batch file:
Like %20, you can use %%%20.
git clone "https:// abc.com /D%%%220an"

Problem with quoted filenames in Batch

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?

Resources