The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream

    20140508-Delphi-2007--Project-Options--Cannot-Edit-Application-Title-HelpFile-Icon-Theming

    20140430-Fiddler-Filter-Actions-Button-Run-Filterset-now

    20140424-Windows-7-free-disk-space

    More Photos
  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,329 other followers

Stopping percentage expansion in a Windows batch file

Posted by Jeroen Pluimers on 2010/04/09

A while ago, I asked a question on percentage expansion in batch-files on superuser.com: a great site with great answers, similar to stackoverflow.com and serverfault.com , but now for asking “Power User” kind of questions.

The percentage sign (%) in URL‘s is to escape (or  URLencode) characters that should not be in a URL itself.
(Note that the old new thing had a very interesting article on URL encoding: there are many different opinions on how to to this ‘right’, and a few of these ‘right’ opinions are not always compatible with each other).

In Windows batch-files and the command-line, the percentage sign is used to expand environment variables, arguments and for loop indexes.
To make life ‘easier’, inside a batch-file, the percentage sign has a slightly different meaning than on the command-line itself.

This can break your batch-files when you use URL encoded parameters.
It does not matter if these parameters are quoted or not: cmd.exe expands them, unless you escape them properly.

So, the command for downloading the URL with wget (similar to curl) differs from running it on the plain command-line or in a batch-file.

Escaping percentage in batch-files

So the best way to escape percentages in batch files is to double them: each % becomes %%.
There is even a very old (MS-DOS era!) knowledge base article about this topic, that I just found when doing the research for this blog article :-)

URL decode

As a sidenote: manually decode thesed escaped URL’s is always a pain.
There are many sites that can do URL decoding on-line.

PS: This was the original question: How can I stop percentage expansion in a batch file? – Super User.

3 Responses to “Stopping percentage expansion in a Windows batch file”

  1. ASB said

    You can also escape the % symbols with a ^% for every %

  2. Andreas Haist said

    Oh Man… After hours of try and error you point me to the solution!!!!

    Thanks :-)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 1,329 other followers

%d bloggers like this: