| 1. |
Solve : Does %1 have limitation on certain characters? |
|
Answer» That looks just LIKE what I was looking for. Also at the end the echo %url% requires to be echo !url! on my system. 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 |
|