1.

Solve : Does %1 have limitation on certain characters?

Answer»

That looks just LIKE what I was looking for.
Need to try it out. It will take a little while.
Thanks a lotCorrection (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!=^ !
)

I get an error:
'The syntax of the command is incorrect.'
I copied and pasted.
Can you see something wrong in the code?
ThanksCode: [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 =&, ;;;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.
Quote from: Frank on July 07, 2012, 05:45:12 PM

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
[ \[
] \]
" \"



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.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.
Quote from: Frank on July 08, 2012, 02:01:06 AM
I thought it was a mistake.

In general, I test scripts before posting them. That does not mean I don't make mistakes!
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"

WOW, you've been busy.
This is great. Just tried it.
Thanks again.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%"

This will make it a little easier to figure out how it works.
Great


Discussion

No Comment Found