1.

Solve : Display special char in env variable??

Answer»

Win2k Sp.4 - Cmd.exe

Code: [Select]@echo off
cls
set a=

set a=%1
echo %a%
This code works if %1 is say -10 or +10 but if %1 is =10 the "=" is not displayed. I assume this is because the char needs to be escaped in the Echo statement but how to do that

Thanks.
Quote from: Hedonist

I assume this is because the char needs to be escaped in the Echo

When you assume....

It's no good trying to escape the vanished character in the batch, because cmd.exe has already stripped it off. You could protect it by wrapping the whole parameter in quotes and then using the ~ variable modifier in the batch to take them off.

Showme.bat

Code: [Select]@echo off
set parameter=%~1
echo parameter is %parameter%

Output of Showme.bat

Code: [Select]S:\Test\Batch>showme "=1"
parameter is =1
The ~ variable modifier strips any surrounding quotes (if present) from a loop variable or from a passed parameter.

It is documented in the help for FOR, so type FOR /? for details.

THis method doesn't work with all problem characters, for EXAMPLE &.



Thank you Dias.

In a reply on post #68095 you escaped all of the ( and ) characters Quote
echo Yesterdate = (Date^(^)- 1^)>yesterday.vbs
echo Yyyy = DatePart^("YYYY", Yesterdate^)>>yesterday.vbs
echo Mm = DatePart^("M" , Yesterdate^)>>yesterday.vbs
echo Dd = DatePart^("D" , Yesterdate^)>>yesterday.vbs

Is there a SPECIAL reason for doing this?The batch file in question works fine without those carets, as I have just now discovered. There are other situations where you do need to escape parentheses so I have GOT in the habit of doing so. If a parenthesis doesn't need ESCAPING, it does no harm to escape it, whereas a parenthesis that should be escaped, but isn't, causes an error.

In the code below, the regular expression passed to SED contains parentheses, and because the command invoking SED is inside the parentheses of a FOR command, they need escaping to stop cmd.exe borking.

@echo off
setlocal enabledelayedexpansion
for /f %%i in (qd.txt) do for /f %%J in ( ' echo %%i ^| sed -e
s/.*\x22\^(.*\^)\x22.*/\1/ ' ) do set yfn=%%j



Thanks again. I understand the need for escaping character(s) in the For command, just couldn't get to grips with escaping ( and ) in the Echo command.



Discussion

No Comment Found