Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Encode in Windows Batch Script

I have a Windows batch script that I use to do quick Google searches. However, I can't figure out how to generically encode special characters. Like if I try to search for C#, the pound sign breaks it. Here is my code:

SET q="https://www.google.com/#q=%*"
SET q=%q: =+%
chrm %q%
like image 913
RobP2 Avatar asked Feb 03 '16 20:02

RobP2


People also ask

What is %% A in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is %20 URL encode?

URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

What is %% g'in batch file?

%%parameter : A replaceable parameter: in a batch file use %%G (on the command line %G) FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'.

What is %~ dp0 in batch script?

The %~dp0 (that's a zero) variable when referenced within a Windows batch file will expand to the drive letter and path of that batch file. The variables %0-%9 refer to the command line parameters of the batch file. %1-%9 refer to command line arguments after the batch file name. %0 refers to the batch file itself.


2 Answers

Without installing any external tools:

@echo off
setlocal


set "string=gibberish+?blahblah@"


:: Define simple macros to support JavaScript within batch
set "beginJS=mshta "javascript:code(close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write("
set "endJS=)));""



:: FOR /F does not need pipe
for /f %%N in (
  '%beginJS% encodeURIComponent("%string%") %endJS%'
) do set encoded=%%N
echo %string% -^> %encoded%
like image 55
npocmaka Avatar answered Sep 21 '22 22:09

npocmaka


I don't think there is a good way to do that directly in a Windows bat script. Python is a great solution for some heavier things like that, and it is cross platform which is always nice. Since you are in Windows, you could probably write a powershell script to do it. However, here is a Python 3 script I wrote which I think does what you are looking for.

import sys
import subprocess
import urllib.parse

browser = sys.argv[1]
browserParms = sys.argv[2]

queryString = " ".join(sys.argv[3:])
queryString = urllib.parse.quote(queryString)
url = "https://www.google.com/#q=" + queryString

subprocess.Popen([browser, browserParms, url])

sys.exit()

Here is a native script for Linux where you can set up your specifics. You could do something very similar in a windows batch file. I named it goog (with no extension because that would be too much to type :) )

#!/bin/bash
python3 /home/justin/Dropbox/MyFiles/Programs/CrossPlatform/Python3/GoogleSearch.py "firefox" "-new-window" "$@"

Make sure your native script location is in $PATH. Execute like this from Terminal or Run A Command.

goog i like turtles
like image 32
Justin Pavatte Avatar answered Sep 20 '22 22:09

Justin Pavatte