Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Display special char in env variable?  (Read 3612 times)

0 Members and 1 Guest are viewing this topic.

Hedonist

    Topic Starter


    Intermediate

    Display special char in env variable?
    « on: October 25, 2008, 08:18:56 PM »
    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.

    Dias de verano

    • Guest
    Re: Display special char in env variable?
    « Reply #1 on: October 26, 2008, 02:29:19 AM »
    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 &.




    Hedonist

      Topic Starter


      Intermediate

      Re: Display special char in env variable?
      « Reply #2 on: October 26, 2008, 02:32:15 PM »
      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?

      Dias de verano

      • Guest
      Re: Display special char in env variable?
      « Reply #3 on: October 26, 2008, 02:57:03 PM »
      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




      Hedonist

        Topic Starter


        Intermediate

        Re: Display special char in env variable?
        « Reply #4 on: October 26, 2008, 03:48:30 PM »
        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.