'Creating a batch file that opens google chrome and puts input into the search box?

So I am just playing with batch files and was curious if it was possible to create a batch file that opens the google browser and without typing into the search box, a variable from my batch file gets put into the search box. Anyone know if that's possible? Thanks.

@echo off
cd c:\program files (x86)\google\application
start chrome.exe www.youtube.com

I can open the web browser, I can even change the code to store the variable, but need to know how to send that variable to the search engine. Youtube i just the website i left it at.



Solution 1:[1]

If you use google as search engine, try to pass the keyword like this :

@echo off
start "" chrome.exe www.google.com#q=batch

and if you want add more than a keyword just add the sign +

@echo off
start "" chrome.exe www.google.com#q=batch+vbscript+HTA

Solution 2:[2]

You could send raw HTTP request as follows:

start "" "c:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "https://www.youtube.com/results?search_query=simon's cat"

Below is possible approach how-to make it more readable in a batch script (non-systematic approach, as e.g. site variable joins together protocol and host name). Use appropriate value of engine variable for a particular host (as it could vary for different servers):

@ECHO OFF >NUL
SETLOCAL EnableExtensions EnableDelayedExpansion
set "chromepath=c:\Program Files (x86)\Google\Chrome\Application" path to chrome
set "site=https://www.youtube.com"
set "engine=results?search_query"                              
set "search=simon's cat"                                       string to search
start "" "!chromepath!\chrome.exe" "!site!/!engine!=!search!"

Delayed expansion used as any variable in above code could contain cmd poisonous characters.

Solution 3:[3]

If you want the simplest alternative, you can just copy the full link and paste it in:

@echo off
start chrome "youtube.com/results?search_query=funny+videos"

To open another tab, separate first address with a space and type "google.com" next to it. If you want to open another browser like FireFox at the same time, type on a new line: start firefox "stackoverflow.com"

It'll look something like this:

@echo off
start chrome "youtube.com/results?search_query=funny+videos" "google.com"
start firefox "stackoverflow.com"

Solution 4:[4]

@echo off
set tmp="%*"
IF %tmp% == "" (
        GOTO :query
    ) ELSE (
        GOTO :replace
    )

:replace
set url=%*
REM set url=%url: =+%
echo %url%
GOTO :search


:query
set /p url=Input search Keywords:
GOTO :search


:search
echo Search query confirmed: %*
echo Attaching to process..
tasklist /nh|findstr "chrome.exe" && start "" "chrome.exe" "? %url%"
REM tasklist /nh|findstr "chrome.exe" && start "" "chrome.exe" "www.google.com/search?q=%url%"

Here is the batch file I use for accomplishing this. It will attach the search tab to an open process of chrome and search for %* arguments. If you don't pass arguments, it will ask for some.

> url installing pycharm on ubuntu 

There is a commented out method aswell that replaces spaces with '+', then searches with raw HTTP instead of the "? %url%" option.
Delete REM on line 12 and 25, and all of line 24 to switch

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hackoo
Solution 2 JosefZ
Solution 3 fleiteh
Solution 4 John Edens