2

I'm trying to write a batch file to open gmail in incognito and enter my credentials automatically when the pc starts up. Based on this post I have the following:

@echo off
cls
start %ProgramFiles(x86)%\Google\Chrome\Application\Chrome.exe --incognito "https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier"
exit

However, when I double-click the .bat file, a cmd window appears with an error window in front that says:

Windows cannot find 'C:\Program'. Make sure you typed it correctly, and then try again.

I think cmd is evaluating %ProgramFiles(x86)% without putting quotes around it or something. I'm new to writing bat files, so I'm not sure how to fix this.

Here's some deets about my system:

Windows 10 Pro 
V. 1607 
OS Build 14393.479
64-bit
Surface Book

3 Answers3

5

Even If you are using the Environment var

%ProgramFiles(x86)%

usually expands to

C:\Program Files (x86)

Paths having spaces in them have to be enclosed in quotes

Second is that Start uses the first argument in Quotes as title. See Help Start

This changed batch works as expected:

@echo off
cls
start "" "%ProgramFiles(x86)%\Google\Chrome\Application\Chrome.exe" --incognito "https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier"
exit
studiohack
  • 13,477
LotPings
  • 7,391
1

The answer from that post was using the user path rather than Program Files (x86). Therefore it makes the unfortunate assumption that there won't be a space in the path.

%userprofile%\AppData\Local\Google\Chrome\Application\Chrome.exe

Since "Program Files (x86)" has a space, you need to surround the path in quotes.

"%ProgramFiles(x86)%\Google\Chrome\Application\Chrome.exe"

CConard96
  • 1,329
  • 1
  • 11
  • 12
0

Since the expansion of {%Programfiles(x86)%} includes a space, you must have quotation marks around it. As a result, you must double the quotation marks you already have in the command line, resulting in a line looking like this:

start "%ProgramFiles(x86)%\Google\Chrome\Application\Chrome.exe --incognito ""https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier"""

Note that it has been quite a while since I needed to do this, and the correct answer could be this one, where the command and the parameter are separately quoted:

start "%ProgramFiles(x86)%\Google\Chrome\Application\Chrome.exe --incognito" "https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier"

One of these will work. I just can't remember which one right now.