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

Author Topic: Does %1 have limitation on certain characters  (Read 10593 times)

0 Members and 1 Guest are viewing this topic.

Frank

    Topic Starter


    Intermediate

    Thanked: 3
    Re: Does %1 have limitation on certain characters
    « Reply #15 on: July 07, 2012, 01:58:09 AM »
    That looks just like what I was looking for.
    Need to try it out. It will take a little while.
    Thanks a lot

    Salmon Trout

    • Guest
    Re: Does %1 have limitation on certain characters
    « Reply #16 on: July 07, 2012, 02:02:34 AM »
    Correction (important!)

    I forgot to make the all occurences of var delayed-expansion ( !var! ) in each line in the loop


    for %%A in (\^" equal and comma semi space) do (
       set var=%%A
       if !var!==\"    set url=!url:!var!=!
       if !var!==equal set url=!url:!var!==!
       if !var!==and   set url=!url:!var!=^&!
       if !var!==comma set url=!url:!var!=^,!
       if !var!==semi  set url=!url:!var!=^;!
       if !var!==space set url=!url:!var!=^ !
       )


    Frank

      Topic Starter


      Intermediate

      Thanked: 3
      Re: Does %1 have limitation on certain characters
      « Reply #17 on: July 07, 2012, 04:19:47 AM »
      I get an error:
      'The syntax of the command is incorrect.'
      I copied and pasted.
      Can you see something wrong in the code?
      Thanks

      Salmon Trout

      • Guest
      Re: Does %1 have limitation on certain characters
      « Reply #18 on: July 07, 2012, 02:18:27 PM »
      Code: [Select]

      @echo off
      setlocal enabledelayedexpansion
      set url=\"equalandcommaspacesemisemisemi\"
      set ReplaceList=equal and comma semi space

      echo Start
      echo url %url%
      echo.

      echo Remove first two characters
      set url=%url:~2%
      echo url %url%
      echo.

      echo Remove final two characters
      set url=%url:~0,-2%
      echo url %url%
      echo.

      echo.
      echo In loop...
      for %%A in (%ReplaceList%) do (
      set search=%%A
      if "!search!"=="equal" set repl==
      if "!search!"=="and"   set "repl=^&"
      if "!search!"=="comma" set repl=,
      if "!search!"=="space" set "repl= "
      if "!search!"=="semi"  set repl=;
      for %%b in ("!search!") do for %%c in ("!repl!") do SET "url=!url:%%~b=%%~c!"
      echo url !url!
      )

      echo.
      echo Finished loop
      echo url %url%


      Code: [Select]
      Start
      url \"equalandcommaspacesemisemisemi\"

      Remove first two characters
      url equalandcommaspacesemisemisemi\"

      Remove final two characters
      url equalandcommaspacesemisemisemi


      In loop...
      url =andcommaspacesemisemisemi
      url =^&commaspacesemisemisemi
      url =^&,spacesemisemisemi
      url =^&,space;;;
      url =^&, ;;;

      Finished loop
      url =&, ;;;

      Frank

        Topic Starter


        Intermediate

        Thanked: 3
        Re: Does %1 have limitation on certain characters
        « Reply #19 on: July 07, 2012, 05:45:12 PM »
        This works well.
        Very tricky loop inside the main loop.
        Just a minor thing, the line in the loop with "repl=^&", doesn't require the inverted commas or the '^'.
        Also at the end the echo %url% requires to be echo !url! on my system.
        With echo %url% I just get '='.
        But, I now have it working.
        Thanks a lot.

        Salmon Trout

        • Guest
        Re: Does %1 have limitation on certain characters
        « Reply #20 on: July 08, 2012, 12:14:37 AM »
        Also at the end the echo %url% requires to be echo !url! on my system.
        With echo %url% I just get '='.

        That is because you changed set "repl=^&" to set repl=&. Quotes and caret are both necessary if you want to use %url% at the end. Batch scripting has special characters and & in a string is notorious. (Did you not see the output listing I made?)

        Briefly, when the command interpreter encounters certain "special characters" it treats them as control characters unless you make special arrangements. Usually this means "escaping" them. The caret (^) is the escape used for most special characters but some are differently escaped.

        For example (you can try stuff like this out at the prompt)

        A single ampersand is the command separator so an unescaped one means "everything after me is a new command" e.g. cls & dir & echo hello world

        1. Ampersand (&) unescaped, setting the variable fails

        Code: [Select]
        c:\>set string=cats & dogs
        'dogs' is not recognized as an internal or external command,
        operable program or batch file.

        c:\>

        2. Escaped in the SET statement, so that works...

        Code: [Select]
        c:\>set string=cats ^& dogs

        c:\>

        but look what happens when we try to expand the variable

        Code: [Select]
        c:\>echo %string%
        cats
        'dogs' is not recognized as an internal or external command,
        operable program or batch file.

        c:\>


        3. We both escape the & with a caret and enclose the assignment portion of the SET statement with quotes

        The assignment works...

        Code: [Select]
        c:\>set "string=cats ^& dogs"

        c:\>

        So does the expansion...

        Code: [Select]
        c:\>echo %string%
        cats & dogs

        c:\>

        Excellent page at http://www.robvanderwoude.com/escapechars.php where I got this table:


        Escape Characters

        Character to be  Escape    Remark
        to be escaped    sequence

           %              %%       May not always be required in doublequoted strings, just try
           ^              ^^       May not always be required in doublequoted strings, but it won't hurt
           &              ^&       
           <              ^<       
           >              ^>       
           |              ^|       
           '              ^'       Required only in the FOR /F "subject" (i.e. between the parenthesis), unless backq is used
           `              ^`       Required only in the FOR /F "subject" (i.e. between the parenthesis), if backq is used
           ,              ^,       Required only in the FOR /F "subject" (i.e. between the parenthesis), even in doublequoted strings
           ;              ^;       
           =              ^=       
           (              ^(       
           )              ^)       
           !              ^^!      Required only when delayed variable expansion is active
           \              \\       Required only in the regex pattern of FINDSTR
           [              \[       
           ]              \]       
           "              \"       



        « Last Edit: July 08, 2012, 12:59:53 AM by Salmon Trout »

        Frank

          Topic Starter


          Intermediate

          Thanked: 3
          Re: Does %1 have limitation on certain characters
          « Reply #21 on: July 08, 2012, 02:01:06 AM »
          Yes I noticed the '^' in your output listing. Not understanding the significance of the &, I thought it was a mistake. That's why I mentioned it.

          Your explanation and description clears it up. I bookmarked the link you supplied. Without experts such as yourself, there would be no point in posting in forums such this.
          I have learned a lot and have it all working now.
          Thank you very much.

          Salmon Trout

          • Guest
          Re: Does %1 have limitation on certain characters
          « Reply #22 on: July 08, 2012, 02:04:52 AM »
          I am working on a method where you use a table of replace pairs e.g. [equals =] [and &] [comma ,] which would remove the IF tests from the loop and make modification easier.

          Salmon Trout

          • Guest
          Re: Does %1 have limitation on certain characters
          « Reply #23 on: July 08, 2012, 02:05:58 AM »
          I thought it was a mistake.

          In general, I test scripts before posting them. That does not mean I don't make mistakes!

          Salmon Trout

          • Guest
          Re: Does %1 have limitation on certain characters
          « Reply #24 on: July 08, 2012, 02:51:23 AM »
          Note: whichever scheme you use, you will find that if you use simple replacement tokens such as and for &, comma for , etc you may break urls because for example sand will become s& and commander will become either ,nder or comm&er (depending on the order you do the replacement) and so on. This may not matter but if it does I'd use tokens you will never find in the range of urls you expect to process e.g. $$$$$$$and$$$$$$$ or replaceAND or whatever.


          Code: [Select]

          @echo off
          setlocal enabledelayedexpansion
          set url=\"equalandcommaspacesemisemisemi\"
          set ReplaceList=equal and comma semi space

          set url=\"http://www.tek-tips.com/threadminder.cfm?pidequal287andpageequal1\"

          REM Alternative ways of building replace table string
          REM set ReplaceTable="equal:=" "and:^&" "comma:," "semi:;" "space: "

          set ReplaceTable=
          set ReplaceTable=%ReplaceTable% "equal:="
          set ReplaceTable=%ReplaceTable% "and:^&"
          set ReplaceTable=%ReplaceTable% "comma:,"
          set ReplaceTable=%ReplaceTable% "semi:;"
          set ReplaceTable=%ReplaceTable% "space: "

          echo Start
          echo url %url%
          echo.

          echo Remove first two characters
          set url=%url:~2%
          echo url %url%
          echo.

          echo Remove final two characters
          set url=%url:~0,-2%
          echo url %url%
          echo.

          echo In loop...
          for %%A in (%ReplaceTable%) do (
          set "ReplacePair=%%~A"
          for /f "tokens=1,2 delims=:" %%B in ('echo !ReplacePair!') do (
          set "search=%%B"
          set "repl=%%C"
          For %%D in ("!search!") do (
          for %%E in ("!repl!") do (
          SET "url=!url:%%~D=%%~E!"
          )
          )
          echo url !url!
          )
          )       
          echo.

          echo Finished loop

          echo unquoted url !url!

          REM quotes will shield the & from the command interpreter
          echo quoted url   "%url%"




          Code: [Select]
          Start
          url \"http://www.tek-tips.com/threadminder.cfm?pidequal287andpageequal1\"

          Remove first two characters
          url http://www.tek-tips.com/threadminder.cfm?pidequal287andpageequal1\"

          Remove final two characters
          url http://www.tek-tips.com/threadminder.cfm?pidequal287andpageequal1

          In loop...
          url http://www.tek-tips.com/threadminder.cfm?pid=287andpage=1
          url http://www.tek-tips.com/threadminder.cfm?pid=287&page=1
          url http://www.tek-tips.com/threadminder.cfm?pid=287&page=1
          url http://www.tek-tips.com/threadminder.cfm?pid=287&page=1
          url http://www.tek-tips.com/threadminder.cfm?pid=287&page=1

          Finished loop
          unquoted url http://www.tek-tips.com/threadminder.cfm?pid=287&page=1
          quoted url   "http://www.tek-tips.com/threadminder.cfm?pid=287&page=1"

          « Last Edit: July 08, 2012, 03:45:04 AM by Salmon Trout »

          Frank

            Topic Starter


            Intermediate

            Thanked: 3
            Re: Does %1 have limitation on certain characters
            « Reply #25 on: July 08, 2012, 03:47:25 AM »
            Wow, you've been busy.
            This is great. Just tried it.
            Thanks again.

            Salmon Trout

            • Guest
            Re: Does %1 have limitation on certain characters
            « Reply #26 on: July 08, 2012, 05:05:26 AM »
            Tidied up and some (hopefully) clarifying comments...

            Code: [Select]

            @echo off
            REM you need this
            setlocal enabledelayedexpansion

            set url=\"http://www.tek-tips.com/threadminder.cfm?pidequal287andpageequal1\"

            REM Alternative ways of building replace table string
            REM (1) all in one line
            REM set ReplaceTable="equal:=" "and:^&" "comma:," "semi:;" "space: "

            REM (2) Start with blank string and append elements one by one (note spaces)
            set ReplaceTable=
            set ReplaceTable=%ReplaceTable% "equal:="
            set ReplaceTable=%ReplaceTable% "and:^&"
            set ReplaceTable=%ReplaceTable% "comma:,"
            set ReplaceTable=%ReplaceTable% "semi:;"
            set ReplaceTable=%ReplaceTable% "space: "

            echo Start
            echo url %url%
            echo.

            echo Remove first two characters which are \"
            set url=%url:~2%
            echo url %url%
            echo.

            echo Remove final two characters which are \"
            set url=%url:~0,-2%
            echo url %url%
            echo.

            echo In loop...
            REM read each element of replace table; the spaces betwen the elements are the delimiters
            for %%A in (%ReplaceTable%) do (

            REM remove quotes using ~ variable modifier
            set "ReplacePair=%%~A"

            REM Split pair into 2 tokens at : character (arbitrarily chosen)
            for /f "tokens=1,2 delims=:" %%B in ('echo !ReplacePair!') do (

            REM Prepare string substitution
            REM Search is first (explicit) loop variable %%B
            set "search=%%B"

            REM Replace is second (implicit) loop variable %%C
            set "repl=%%C"

            REM This is a trick to use variables in string substitution
            For %%D in ("!search!") do (
            for %%E in ("!repl!") do (

            REM Execute string substitution
            SET "url=!url:%%~D=%%~E!"

            )
            )

            echo url !url!

            )
            )       
            echo.

            echo Finished loop

            REM Delayed expansion or quotes will shield the & from the command interpreter
            echo unquoted url !url!
            echo quoted url   "%url%"


            Frank

              Topic Starter


              Intermediate

              Thanked: 3
              Re: Does %1 have limitation on certain characters
              « Reply #27 on: July 08, 2012, 05:39:33 AM »
              This will make it a little easier to figure out how it works.
              Great