Convert Text to Percent Encoded (Hex) Url

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
NYTReader123
Posts: 9
Joined: 15 Sep 2008 17:20

Convert Text to Percent Encoded (Hex) Url

#1 Post by NYTReader123 » 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

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 22 Sep 2008 16:20

NYTReader123

Code: Select all

:process 
SETLOCAL DISABLEDELAYEDEXPANSION
set "NewName=%~1"

set "NewName=%NewName:"=%"
set "NewName=%NewName:&=`26%"
set "NewName=%NewName:ö=`C3`B6%"
set "NewName=%NewName:!=`21%"

SETLOCAL ENABLEDELAYEDEXPANSION
set "NewName=!NewName:%%=`25!"
for /L %%A in (0,1,200) do (
    if "!NewName:~%%A,1!"=="*" (
        set /a Aplus1=%%A+1
        call set "NewName=%%NewName:~0,%%A%%`2A%%NewName:~!Aplus1!%%"
    )
)
set "NewName=!NewName:`=%%!"
ENDLOCAL&set "NewName=%NewName%"

rem echo."%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


Some notes:
1. Processing the "!" requires DELAYEDEXPANSION to be disabled.
2. Replacing "*" only works for the first 200 characters in the string. If your strings are longer just use a bigger number in the "for /L" loop.
3. The code assumes that the "`" character doesn't appear in any of your string and it's used as special character here to avoid conflicts with varaibles.

DOS IT HELP? :wink:

or like this:

Code: Select all

:process 
SETLOCAL DISABLEDELAYEDEXPANSION
set "NewName=%~1"

set "NewName=%NewName:"=%"
set "NewName=%NewName:!=`21%"

SETLOCAL ENABLEDELAYEDEXPANSION
set "NewName=!NewName:%%=`25!"
for /L %%A in (0,1,200) do (
      set /a Aplus1=%%A+1
      if 0==1 ( rem
      ) ELSE if "!NewName:~%%A,1!"=="*" (call set "NewName=%%NewName:~0,%%A%%`2A%%NewName:~!Aplus1!%%"
      ) ELSE if "!NewName:~%%A,1!"=="&" (call set "NewName=%%NewName:~0,%%A%%`26%%NewName:~!Aplus1!%%"
      ) ELSE if "!NewName:~%%A,1!"=="ö" (call set "NewName=%%NewName:~0,%%A%%`C3`B6%%NewName:~!Aplus1!%%"
      )
)
set "NewName=!NewName:`=%%!"
ENDLOCAL&set "NewName=%NewName%"

rem echo."%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

NYTReader123
Posts: 9
Joined: 15 Sep 2008 17:20

#3 Post by NYTReader123 » 24 Sep 2008 18:20

Thanks--this works great!
I doubt I would have come up with any of your slick tricks (looping to replace the "*", using the "'" placeholder, or even mixing the DISABLED/ENABLEDELAYEDEXPANSION).

In fact the only change I had to make was in the placeholder character since the crazy file I am using has several "'" --> fortunately no "¬" which works just as well.

Again thanks for the great solution.

--Koleman

Post Reply