Loading ...
Sorry, an error occurred while loading the content.
 

How to escape special characters?

Expand Messages
  • Parag Doke
    Hello All. I m stuck at one point while trying to write a script. The problem was seen as a part of a larger script ... but I ll post the relevant part here.
    Message 1 of 5 , Dec 1, 2010
      Hello All.
      I'm stuck at one point while trying to write a script. The problem was seen as a part of a larger script ... but I'll post the relevant part here.

      =====temp.txt=====
      a< b> c| d^ e% f&
      =====temp.txt=====

      =====temp.bat=====
      set /p "var=" < "temp.txt"

      echo "%var%"

      set "new=%var:<=<#%"
      set "new=%new:>=>#%"
      set "new=%new:|=|#%"
      set "new=%new:&=&#%"
      set "new=%new:^%=^%%#%"
      rem call set "new=%%new:^=^#%%"

      echo "%new%"
      =====temp.bat=====
      I am able to see the escaped characters < > | and & ... and I see the hash sign added after each of them. But somehow not able to figure out how to escape the % sign. And yet to attempt the ^ caret.

      Can someone kindly share the trick ?
      Thanks in advance,
      Parag Doke
    • foxidrive
      ... Caret is treated the same way as the rest. Percent is an ugly character to manipulate in batch - can you describe the circumstance in which you need to
      Message 2 of 5 , Dec 1, 2010
        On 2/12/2010 02:44, Parag Doke wrote:
        > Hello All.
        > I'm stuck at one point while trying to write a script. The problem was seen as a part of a larger script ... but I'll post the relevant part here.
        >
        > =====temp.txt=====
        > a< b> c| d^ e% f&
        > =====temp.txt=====
        >
        > =====temp.bat=====
        > set /p "var="< "temp.txt"
        >
        > echo "%var%"
        >
        > set "new=%var:<=<#%"
        > set "new=%new:>=>#%"
        > set "new=%new:|=|#%"
        > set "new=%new:&=&#%"
        > set "new=%new:^%=^%%#%"
        > rem call set "new=%%new:^=^#%%"
        >
        > echo "%new%"
        > =====temp.bat=====
        > I am able to see the escaped characters< > | and& ... and I see the hash sign added after each of them. But somehow not able to figure out how to escape the % sign. And yet to attempt the ^ caret.
        >
        > Can someone kindly share the trick ?

        Caret is treated the same way as the rest. Percent is an ugly character
        to manipulate in batch - can you describe the circumstance in which you
        need to use it and we'll see if there is a workaround.


        @echo off
        set /p "var=" < "temp.txt"

        echo "%var%"

        set "new=%var:<=<#%"
        set "new=%new:>=>#%"
        set "new=%new:|=|#%"
        set "new=%new:&=&#%"
        set "new=%new:^=^#%"

        echo "%new%"
        pause
      • Parag Doke
        Hello Mic. Thanks for the input regarding the caret. Here is the scenario: I m using cleartool to list the comment of a file version (this comment would be
        Message 3 of 5 , Dec 1, 2010
          Hello Mic.
          Thanks for the input regarding the caret.

          Here is the scenario:
          I'm using cleartool to list the comment of a file version (this comment
          would be entered by whoever checked in the file version ... so it is likely
          to have any set of characters ... we'll assume ASCII for now). So I capture
          the comment to a text file using a command similar to:
          cleartool desc -fmt "%%c" "<file version>" > SourceComment.txt
          The cleartool syntax for the comment is to use %c, but since I do this
          within a batch file, I need to use double % signs.

          For each line in the comment, I need to substitute the name of the person
          who created that file version with a blank string (since this would be on my
          own branch). For this I need to iterate over each line, substitute the name
          with a blank string and then append it to another output file (which will be
          used at a later point).

          Sample SourceComment.txt
          {person_name} This is the user entered comment.
          It can have multiple lines &
          special characters such as ^ < > | % & too. (script_name)

          Sample OutputFile.txt
          This is the user entered comment.
          It can have multiple lines &
          special characters such as ^ < > | % & too.

          The person_name will be on the 1st line. Also the script_name string will be
          the last set of characters. What I am trying to state is that substitution
          might be necessary only on the 1st and the last line.

          Now I am thinking I might use the +n feature of more command to process the
          lines in the middle faster (than taking up each line for processing). But
          still it does not solve the problem of having the special characters in the
          1st and/or the last line.

          I admit using sed / perl / ant for substitution is a far far better option.
          I will keep that as the last option if nothing else works out. I would
          probably go for VBScript/JavaScript using cscript as it mostly won't need
          any additional installation.

          Copying my script (so far) below. Going by the nature of what has been
          previously shared over this forum, my script might look really really dumb
          :-(. Feel free to comment wherever needed.
          Thanks in advance !

          Here is what I have managed so far:


          @if not defined debug echo off
          :GetFileComment
          cleartool desc -fmt "%%c" "%SourceFile%" > "%Temp%\SourceComment.txt"

          echo ===Earlier comment===
          type "%Temp%\SourceComment.txt"

          if exist "%OutputFile%.interim.txt" del "%OutputFile%.interim.txt"

          rem Note: Lines in comments can have any special chars.
          rem Found that passing the line using call :Function "%%a" leads to
          problems.
          rem Chars found to have problems are :(colon) "(quotes).
          for /f "delims=" %%a in ('type "%Temp%\SourceComment.txt"') do (
          echo.%%a> "%temp%\CurrentLine.txt"
          call :ProcessLine "%Person_Name%" "%temp%\CurrentLine.txt"
          type "%temp%\CurrentLine.txt" >> "%OutputFile%.interim.txt"
          )

          copy /y "%OutputFile%.interim.txt" "%OutputFile%" > nul

          echo ===Fixed comment===
          type "%OutputFile%"
          goto Finish

          :ProcessLine
          set "PersonNameToRemove=%~1"
          set "FileToUse=%~2"
          if defined debug echo Removing name %PersonNameToRemove% from %FileToUse%
          set /p CurrentLine=< "%FileToUse%"
          if defined debug echo Working on line "%CurrentLine%"

          rem Note that following 2 replaces are done using call because the text to
          be replaced is uncertain.
          call set "CurrentLine=%%CurrentLine:{%PersonNameToRemove%} =%%"
          call set "CurrentLine=%%CurrentLine:{%PersonNameToRemove%}=%%"
          if defined debug echo "%CurrentLine%"
          set "CurrentLine=%CurrentLine: (script_name)=%"
          set "CurrentLine=%CurrentLine:(script_name)=%"
          if defined debug echo "%CurrentLine%"

          rem Escape special characters & < > | ^
          rem Don't know how to escape %
          set "CurrentLine=%CurrentLine:&=^&%"
          set "CurrentLine=%CurrentLine:<=^<%"
          set "CurrentLine=%CurrentLine:>=^>%"
          set "CurrentLine=%CurrentLine:|=^|%"
          set "CurrentLine=%CurrentLine:^=^^%"
          if defined debug echo "%CurrentLine%"

          rem This line needs the special characters to be escaped, else it won't echo
          properly.
          rem If we can somehow get this echo to work fine with special characters,
          then the substitution won't be needed.
          echo.%CurrentLine%> "%FileToUse%"
          goto EOF


          On Thu, Dec 2, 2010 at 5:14 AM, foxidrive <foxidrive@...> wrote:

          >
          >
          > On 2/12/2010 02:44, Parag Doke wrote:
          > > Hello All.
          > > I'm stuck at one point while trying to write a script. The problem was
          > seen as a part of a larger script ... but I'll post the relevant part here.
          > >
          > > =====temp.txt=====
          > > a< b> c| d^ e% f&
          > > =====temp.txt=====
          > >
          > > =====temp.bat=====
          > > set /p "var="< "temp.txt"
          > >
          > > echo "%var%"
          > >
          > > set "new=%var:<=<#%"
          > > set "new=%new:>=>#%"
          > > set "new=%new:|=|#%"
          > > set "new=%new:&=&#%"
          > > set "new=%new:^%=^%%#%"
          > > rem call set "new=%%new:^=^#%%"
          > >
          > > echo "%new%"
          > > =====temp.bat=====
          > > I am able to see the escaped characters< > | and& ... and I see the hash
          > sign added after each of them. But somehow not able to figure out how to
          > escape the % sign. And yet to attempt the ^ caret.
          > >
          > > Can someone kindly share the trick ?
          >
          > Caret is treated the same way as the rest. Percent is an ugly character
          > to manipulate in batch - can you describe the circumstance in which you
          > need to use it and we'll see if there is a workaround.
          >
          > @echo off
          >
          > set /p "var=" < "temp.txt"
          >
          > echo "%var%"
          >
          > set "new=%var:<=<#%"
          > set "new=%new:>=>#%"
          > set "new=%new:|=|#%"
          > set "new=%new:&=&#%"
          > set "new=%new:^=^#%"
          >
          > echo "%new%"
          > pause
          >
          >
          >



          --
          Parag Doke
          Save paper, save trees. Do not print emails/documents unless absolutely
          necessary.


          [Non-text portions of this message have been removed]
        • foxidrive
          ... [snip] As you ve already suggested, a script using cscript is the best option in this case, where any character can appear and needs to be catered for. In
          Message 4 of 5 , Dec 1, 2010
            On 2/12/2010 17:01, Parag Doke wrote:
            > Hello Mic.
            > Thanks for the input regarding the caret.
            >
            > Here is the scenario:
            > I'm using cleartool to list the comment of a file version (this comment
            > would be entered by whoever checked in the file version ... so it is likely
            > to have any set of characters ... we'll assume ASCII for now). So I capture
            > the comment to a text file using a command similar to:
            > cleartool desc -fmt "%%c" "<file version>"> SourceComment.txt
            > The cleartool syntax for the comment is to use %c, but since I do this
            > within a batch file, I need to use double % signs.
            >
            > For each line in the comment, I need to substitute the name of the person
            > who created that file version with a blank string (since this would be on my
            > own branch). For this I need to iterate over each line, substitute the name
            > with a blank string and then append it to another output file (which will be
            > used at a later point).
            >
            > Sample SourceComment.txt
            > {person_name} This is the user entered comment.
            > It can have multiple lines&
            > special characters such as ^< > | %& too. (script_name)
            >
            > Sample OutputFile.txt
            > This is the user entered comment.
            > It can have multiple lines&
            > special characters such as ^< > | %& too.
            >
            > The person_name will be on the 1st line. Also the script_name string will be
            > the last set of characters. What I am trying to state is that substitution
            > might be necessary only on the 1st and the last line.
            >
            > I admit using sed / perl / ant for substitution is a far far better option.
            > I will keep that as the last option if nothing else works out. I would
            > probably go for VBScript/JavaScript using cscript as it mostly won't need
            > any additional installation.

            [snip]

            As you've already suggested, a script using cscript is the best option
            in this case, where any character can appear and needs to be catered for.

            In the future the people that handle your scripts will need to be able
            to manipulate them without knowing the arcane scripting techniques that
            could be used. % is always a problem in batch scripts and I avoid using it.
          • Parag Doke
            Okay. I will go for it then. Thanks for leading me there. Have a nice day ahead ! Regards, Parag Doke PS: I went for JScript to use the try catch ... but I m a
            Message 5 of 5 , Dec 1, 2010
              Okay. I will go for it then. Thanks for leading me there.
              Have a nice day ahead !

              Regards,
              Parag Doke

              PS: I went for JScript to use the try catch ... but I'm a noob there too
              :-). Will make it quit with non zero errorcodes in case of IO exceptions at
              some later date. For now, this is what will work for me.

              WScript.echo("Starting "+WScript.ScriptName);
              var args=WScript.Arguments;
              if (args.length < 3) {
              WScript.echo("Usage: "+WScript.ScriptName+" FilePath
              FindString/FindRegex NewString [NewFilePath]");
              WScript.Quit(1)
              }
              var fso=WScript.CreateObject("Scripting.FileSystemObject");
              var forReading=1;
              var forWriting=2;
              if (fso.GetFile(args.Item(0)).Size > 0) {
              var text=fso.OpenTextFile(args.Item(0),forReading).ReadAll();
              text=text.replace(args.Item(1),args.Item(2));
              if (args.Item(3).length > 0) {
              fso.OpenTextFile(args.Item(3),forWriting,true).Write(text);
              } else {
              WScript.echo(text);
              }
              }
              WScript.echo("Finished "+WScript.ScriptName);


              On Thu, Dec 2, 2010 at 11:59 AM, foxidrive <foxidrive@...> wrote:

              >
              >
              > On 2/12/2010 17:01, Parag Doke wrote:
              > > Hello Mic.
              > > Thanks for the input regarding the caret.
              > >
              > > Here is the scenario:
              > > I'm using cleartool to list the comment of a file version (this comment
              > > would be entered by whoever checked in the file version ... so it is
              > likely
              > > to have any set of characters ... we'll assume ASCII for now). So I
              > capture
              > > the comment to a text file using a command similar to:
              > > cleartool desc -fmt "%%c" "<file version>"> SourceComment.txt
              > > The cleartool syntax for the comment is to use %c, but since I do this
              > > within a batch file, I need to use double % signs.
              > >
              > > For each line in the comment, I need to substitute the name of the person
              > > who created that file version with a blank string (since this would be on
              > my
              > > own branch). For this I need to iterate over each line, substitute the
              > name
              > > with a blank string and then append it to another output file (which will
              > be
              > > used at a later point).
              > >
              > > Sample SourceComment.txt
              > > {person_name} This is the user entered comment.
              > > It can have multiple lines&
              > > special characters such as ^< > | %& too. (script_name)
              > >
              > > Sample OutputFile.txt
              > > This is the user entered comment.
              > > It can have multiple lines&
              > > special characters such as ^< > | %& too.
              > >
              > > The person_name will be on the 1st line. Also the script_name string will
              > be
              > > the last set of characters. What I am trying to state is that
              > substitution
              > > might be necessary only on the 1st and the last line.
              > >
              > > I admit using sed / perl / ant for substitution is a far far better
              > option.
              > > I will keep that as the last option if nothing else works out. I would
              > > probably go for VBScript/JavaScript using cscript as it mostly won't need
              > > any additional installation.
              >
              > [snip]
              >
              > As you've already suggested, a script using cscript is the best option
              > in this case, where any character can appear and needs to be catered for.
              >
              > In the future the people that handle your scripts will need to be able
              > to manipulate them without knowing the arcane scripting techniques that
              > could be used. % is always a problem in batch scripts and I avoid using it.
              >
              >
              >



              --
              Parag Doke
              Save paper, save trees. Do not print emails/documents unless absolutely
              necessary.


              [Non-text portions of this message have been removed]
            Your message has been successfully submitted and would be delivered to recipients shortly.