The @urlencode function doesn't seem to URL encode

Aug 28, 2008
127
4
The issue is that @urlencode doesn't seem to encode properly. Let's say somebody goes to the main Google page and searches for "C#". This is a parameter that should be URL-encoded as "C%23", which is precisely what the main Google page does. When I use @urlencode[C#] in the latest version of TakeCommand (13.00.28 x64), however, it gives me "23" as a result. The help file makes it sound like it should correctly encode an entire string:

Encode a string for Internet transmission, replacing non-alphanumeric characters with their %xx hex representation.

So I don't understand how it can take "C#" as an argument and spit back "23". Could somebody help me understand? Or is this a bug? Thanks in advance!
 
May 20, 2008
12,167
133
Syracuse, NY, USA
On Thu, 10 Nov 2011 11:09:28 -0500, Phileosophos <> wrote:

|So I don't understand how it can take "C#" as an argument and spit back "23". Could somebody help me understand? Or is this a bug? Thanks in advance!

It seems OK here (13.00.28/32-bit/Win7)/

Code:
v:\> setdos /x-4

v:\> echo %@urlencode[C#]
C%23

v:\> setdos /x+4

v:\>
 
Aug 28, 2008
127
4
Here is the text copied out of a TakeCommand session:

C:\> echo %@urlencode[C#]
C

C:\> echo %@urlencode["C#"]
23

I've checked by using the batch file debugger as well, working on on the assumption that maybe the resulting '%' was somehow screwing up the echo command, but it's not. I can't seem to get a properly encoded URL, and I haven't a clue why.
 

rconn

Administrator
Staff member
May 14, 2008
12,556
167
> Here is the text copied out of a TakeCommand session:
>
> C:\> echo %@urlencode[C#]
> C

WAD -- that gets translated to C%23, and the parser then does further
variable expansion on the result. %23 is interpreted as an empty argument
to ECHO and gets removed.


> C:\> echo %@urlencode["C#"]
> 23

WAD - that gets translated to %22C%23%22, and %22C% is parsed as an empty
argument, 23 is assumed to be a literal argument, and %22 is interpreted as
an empty argument.

ECHO is a bad way to display arguments with embedded %'s.
 
May 20, 2008
3,515
4
Elkridge, MD, USA
rconn wrote:
| ...
| ECHO is a bad way to display arguments with embedded %'s.

This works:

echo "%@urlencode[C#]"

but of course it displays the quotation marks: "C%23".

The SAFEECHO command of C. Dye's plugin SafeChars.dll behave exactly as
ECHO...
--
HTH, Steve
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,689
106
Albuquerque, NM
prospero.unm.edu
rconn wrote:
| ...
| ECHO is a bad way to display arguments with embedded %'s.

This works:

echo "%@urlencode[C#]"

but of course it displays the quotation marks: "C%23".

The SAFEECHO command of C. Dye's plugin SafeChars.dll behave exactly as ECHO...

The args are expanded before SAFEECHO ever sees them. I do, however, have an alternative to @URLENCODE:

HTags Plugin

It uses the same basic idea as SafeChars: the percent signs are replaced with 'fake' percent signs, which get translated back to ASCII when you redirect the resulting string into a file.
 
May 20, 2008
12,167
133
Syracuse, NY, USA
On Thu, 10 Nov 2011 17:25:06 -0500, Steve Fabian <> wrote:

|This works:
|
|echo "%@urlencode[C#]"

That shouldn't work and it doesn't here.

Code:
v:\> echo "%@urlencode[C#]"
"C"

Have you forgotten about a prior "SETDOS /X-4"?
 
May 20, 2008
3,515
4
Elkridge, MD, USA
From: vefatica
| On Thu, 10 Nov 2011 17:25:06 -0500, Steve Fabian <> wrote:
|
|| This works:
||
|| echo "%@urlencode[C#]"
|
| That shouldn't work and it doesn't here.
|...
| Have you forgotten about a prior "SETDOS /X-4"?

I sure did! Discovered long after posting.

This does work with SETDOS/X0 (i.e., interpret everything):
echo %@safeexp[@urlencode[C#]]
and correctly displays C%23. As Charles wrote, SAFEECHO receives the string only after the percent sign is mangled, but @SAFEEXP[] generates it in a safe mode.
--
Steve
 

rconn

Administrator
Staff member
May 14, 2008
12,556
167
> ---Quote (Originally by rconn)---
> ECHO is a bad way to display arguments with embedded %'s.
> ---End Quote---
> Er... what would be a good way to do it?

Well, assuming you actually *needed* to do it (and I doubt you would), I'd
assign the result of the function to a var, turn off expansion with SETDOS,
and then echo it.
 
Aug 28, 2008
127
4
ECHO is a bad way to display arguments with embedded %'s.

I realize that, which is precisely why I mentioned I get the very same thing when using the function in the batch file debugger or anywhere else. I've been trying to come up with a good way to write batch files that launch well-formatted URLs on the basis of user input, and I don't see any good way to use the @urlencode function to do that. If you do, please explain how it's intended to be used; as it is, I don't see how it's useful. Thanks in advance.
 
Nov 2, 2009
302
6
Chile
www.farah.cl
Well, assuming you actually *needed* to do it (and I doubt you would), I'd
assign the result of the function to a var, turn off expansion with SETDOS,
and then echo it.

Sure, it's not typical in a CMD/TCC environment, but in HTML processing it is a common sight; there are many situations where a special char ('%' here) needs to be treated as a regular character. Perhaps there should be a %@percentencode[string] function that will "escape" the percentage (by adding the second '%') so it can be displayed or passed around.
 
May 20, 2008
3,515
4
Elkridge, MD, USA
From: mfarah
| Sure, it's not typical in a CMD/TCC environment, but in HTML
| processing it is a common sight; there are many situations where a
| special char ('%' here) needs to be treated as a regular character.
| Perhaps there should be a %@percentencode[string] function that will
| "escape" the percentage (by adding the second '%') so it can be
| displayed or passed around.

Look in HELP -> TCC -> Variables and Functions -> Functions -> Functions by Category -> Binary Buffers.
--
HTH, Steve
 
Aug 28, 2008
127
4
For sake of clarity, here's the batch file on which I'm working. The point is to enable commands of the form "WebSearch arg1 arg2 arg3" that will invoke a given search engine.

iff %#==0 then
echo Usage: WebSearch arg1 arg2 arg3 ...
quit
endiff

set url=http://www.google.com/search?q=%1

do count=2 to %#
set url=%url%^+%[%count]
enddo

echo Launching search URL: %url%
start %url%
unset url

That works as it is, but it goes wrong when using arguments that need to be URL encoded (e.g., C#). If you can explain how I might put the @urlencode function to work, I'd appreciate it. Thanks!
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,689
106
Albuquerque, NM
prospero.unm.edu
That works as it is, but it goes wrong when using arguments that need to be URL encoded (e.g., C#). If you can explain how I might put the @urlencode function to work, I'd appreciate it. Thanks!

Something like this, perhaps?

Code:
@echo off
setlocal
setdos /x-4

iff %# == 0 then
   echo Usage: WebSearch arg1 arg2 arg3 ...
   quit
endiff

set url=http://www.google.com/search?q=

do count = 1 to %#
   set arg=%[%count]
   set arg=%@urlencode[%arg]
   iff %count == 1 then
      set url=%url%%arg
   else
      set url=%url+%arg
   endiff
enddo

echo Launching search URL: %url
start %url
endlocal
 

Similar threads