Convert Text to Percent Encoded (Hex) Url
Posted: 15 Sep 2008 20:12
Hi:
I have a text file which I am trying to convert to Hex format (I am passing the resulting strings to a wget call, and the website I am calling is finicky about accepting non-standard English characters-- à,ÿ, etc -- or even some English characters-- such as &, *, ?. The website will read "Bj%C3%B6rk" but not "Björk" and "KC%20%26%20The%20Sunshine%20Band" but not "KC & The Sunshine Band").
My batch file uses the code below, but it does not convert any of the resvered DOS characters: %, *, ! (the code just passes these characters on in their original form rather than their hex equivalent). I have tried several permutations, e.g.
set NewName="!NewName:%=%%25!"
set NewName="!NewName:%%=%%25!"
set NewName="!NewName:^%=%%25!"
etc.
I am running this as part of a batch file on a command line Win XP x64 PC.
Can anyone let me know what I am doing wrong? Equally good would be a suggestion of a command line program which converts strings from text to Hex / Percent encoding (rawurlencode works in PHP but I could not find a DOS port)?
Thanks,
Koleman
--------------------- code -------------------------
SETLOCAL ENABLEDELAYEDEXPANSION
fOR /F "tokens=* skip=1" %%a in (artists_test.txt) do (
call :process "%%a"
)
goto :eof
:process
set OldName=%1
set NewName="!OldName:&=%%26!"
set NewName="!NewName:ö=%%C3%%B6!"
set NewName="!NewName:^^!=%%21!"
set NewName="!NewName:%=%%25!"
set NewName="!NewName:*=%%2A!"
REM stripping out all quote marks
set NewName=%NewName:"=%
wget -q -O "%NewName%_similar.xml" "http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=%NewName%&limit=9999&api_key=b25b959554ed76058ac220b7b2e0a026"
goto :eof
I have a text file which I am trying to convert to Hex format (I am passing the resulting strings to a wget call, and the website I am calling is finicky about accepting non-standard English characters-- à,ÿ, etc -- or even some English characters-- such as &, *, ?. The website will read "Bj%C3%B6rk" but not "Björk" and "KC%20%26%20The%20Sunshine%20Band" but not "KC & The Sunshine Band").
My batch file uses the code below, but it does not convert any of the resvered DOS characters: %, *, ! (the code just passes these characters on in their original form rather than their hex equivalent). I have tried several permutations, e.g.
set NewName="!NewName:%=%%25!"
set NewName="!NewName:%%=%%25!"
set NewName="!NewName:^%=%%25!"
etc.
I am running this as part of a batch file on a command line Win XP x64 PC.
Can anyone let me know what I am doing wrong? Equally good would be a suggestion of a command line program which converts strings from text to Hex / Percent encoding (rawurlencode works in PHP but I could not find a DOS port)?
Thanks,
Koleman
--------------------- code -------------------------
SETLOCAL ENABLEDELAYEDEXPANSION
fOR /F "tokens=* skip=1" %%a in (artists_test.txt) do (
call :process "%%a"
)
goto :eof
:process
set OldName=%1
set NewName="!OldName:&=%%26!"
set NewName="!NewName:ö=%%C3%%B6!"
set NewName="!NewName:^^!=%%21!"
set NewName="!NewName:%=%%25!"
set NewName="!NewName:*=%%2A!"
REM stripping out all quote marks
set NewName=%NewName:"=%
wget -q -O "%NewName%_similar.xml" "http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=%NewName%&limit=9999&api_key=b25b959554ed76058ac220b7b2e0a026"
goto :eof