1

I'm writing a batch file to open Netflix in Google Chrome in fullscreen mode. My knowledge of batch files is infantile, though...

chrome.exe --start-fullscreen --app=https://www.netflix.com

This of course works when run from the same directory as the chrome executable but doesn't work from anywhere else.

I'm looking for a way to write the batch file to search and locate the chrome.exe and then execute it with the above switches. That way I can share the batch file to other people and it will work regardless of their file paths. Can it be done?

--edit--

Since Appleoddity suggests searching is unnecessary, would this be the best way to go about writing a batch file for this purpose?:

@echo off    
cd\program files (x86)\google\chrome\application
chrome.exe --start-fullscreen --app=https://www.netflix.com
1
  • 2
    You don’t need to search for it. Chrome only installs per system in program files or per user in %localappdata%. You can also query registry keys for its installation location. Searching the drive for it will be terribly inefficient. – Appleoddity Jan 11 '18 at 5:23
2

Because people might have program files on an odd drive rather than C (unusual but possible), it's best to use the system variable for the program files folder. Also, as it could be in the local app data folder, we should check there too.

rem :: set file location

if exist %localappdata%\google\chrome\application\chrome.exe (
 set chrome_exe="%localappdata%\google\chrome\application\chrome.exe"
)  

if exist %PROGRAMFILES(x86)%\google\chrome\application\chrome.exe (
 set chrome_exe="%PROGRAMFILES(x86)%\google\chrome\application\chrome.exe"
 )

if exist %PROGRAMFILES%\google\chrome\application\chrome.exe (
 set chrome_exe="%PROGRAMFILES%\google\chrome\application\chrome.exe"
 ) 

 rem :: run chrome
%chrome_exe% --start-fullscreen --app=https://www.netflix.com
4
  • This is much more concise and answers the original question using “batch,” as requested. It also uses environment variables which is a must. I would just recommend adding an additional else if to check the %programfiles% path also, as you cannot assume the OS will be 64-bit. – Appleoddity Jan 11 '18 at 13:37
  • I was curious of this and attempted to run on 3 systems. All my systems have the Program Files (x86) installation of Google Chrome, however this batch script seemingly attempts to open my %localappdata% on each PC. Could you guys confirm? – DankyNanky Jan 12 '18 at 3:34
  • not sure why the if-else wasn't working right. This new version with separate statements works for me – Sir Adelaide Jan 12 '18 at 5:37
  • On my system Chrome is installed in 'Program Files (x86)' but the batch file is setting the chrome_exe variable to 'Program Files' even though it isn't installed there: "C:\Program Files\google\chrome\application\chrome.exe" --start-fullscreen --app=https://www.netflix.com The system cannot find the path specified. – YOURMOM Jan 12 '18 at 7:30
2

I've written something that works. Which I couldn't have done without Sir Adelaide's post. So thank you, Sir Adelaide, for your example. Also thank you everyone else who contributed; it's been educational :)

( I chose batch over powershell because anyone I wanted to share the powershell script with would first have to change their execution policy settings. Which is not necessary with batch.)

if exist "%localappdata%\google\chrome\application\chrome.exe" set file_found="yes" 
if not exist "%localappdata%\google\chrome\application\chrome.exe" set file_found="no" 
if %file_found%=="yes" set chrome_exe="%localappdata%\google\chrome\application\chrome.exe"

if exist "%programfiles%\google\chrome\application\chrome.exe" set file_found="yes" 
if not exist "%programfiles%\google\chrome\application\chrome.exe" set file_found="no" 
if %file_found%=="yes" set chrome_exe="%programfiles%\google\chrome\application\chrome.exe"

if exist "%programfiles(x86)%\google\chrome\application\chrome.exe" set file_found="yes" 
if not exist "%programfiles(x86)%\google\chrome\application\chrome.exe" set file_found="no" 
if %file_found%=="yes" set chrome_exe="%programfiles(x86)%\google\chrome\application\chrome.exe" 

%chrome_exe% --start-fullscreen --app=https://www.netflix.com
1
  • 1
    YOURMOMMA —o{|||||||]::;;::;;::;;::> Cool name first off.... Secondly, per your comments regarding the need to change execution policy settings, I wanted to point you to my answer here for an example of the way I write batch scripts and incorporate PowerShell logic into it and then I execute PowerShell without manually needing to change the execution policy but rather with the -ExecutionPolicy Bypass so it occurs at execution time or whatever.superuser.com/questions/1282303/… Just FYI more than anything for you. – Drink More Pimp Juice IT Jan 14 '18 at 5:21
1

I would definitely recommend converting to PowerShell (It's easier, in my opinion) and has heaps of cmd-lets to help with things like this.

In this case we assume Google Chrome has 2 install paths, and therefore this is the main variable we need to account for. Using the Test-Path option we can dictate 2 UNC paths for the potential option.

Here is a script that uses the "Test-Path" we can test if Google Chrome exists in Program Files (x86), and if not, we assign the value of LocalAppData.

Here it is:

    $ConfirmGoogleChrome = (Test-Path "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
    $Process = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

        function OpenChromeFullScreen {
        If ($ConfirmGoogleChrome -eq $True) {
        start $Process --start-fullscreen --app="https://www.netflix.com"
        }
        else {
        $Process = "%localappdata%\google\chrome\application\chrome.exe"
        start $Process --start-fullscreen --app="https://www.netflix.com"
        }
   }
OpenChromeFullScreen

Note that PowerShell was enabled by default in Windows Vista (I believe) and onwards, and requires the Net Framework to operate - which should be enabled on most moderns OS installs.

Update

Due to the comments stating this may not be as robust as required (as we cannot assume C:\ is the OS install path), I've done a little "improvement":

# Variables for the HomeDrive (OS Install Path)
# Inclusive of the path to AppData

$OSInstallPath  = $env:HOMEDRIVE
$OSLocalAppData = $env:LOCALAPPDATA

# Performs Checks on the end path

$ChromeProgramFiles = (Test-Path "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe") 
$ChromeLocalAppData = (Test-Path "$OSLocalAppData\google\chrome\application\chrome.exe")

# Variable for Google Chrome Path
# We default this to Program Files
$GoogleChrome = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"


# Function to set the paths
Function ConfirmSetPaths {
    If ($ChromeProgramFiles -eq $False) {
    $GoogleChrome = "$OSLocalAppData\google\chrome\application\chrome.exe" 
  }
}

ConfirmSetPaths
Start "$GoogleChrome" --start-fullscreen --app="https://www.netflix.com"

Essentially we create variables ("$OSInstappPath" and "$OSLocalAppData" which are system linked variables) for the install path on your PC (as the UNC path may change depending on who runs it.

Then we make an additional 2 variables for the two paths where chrome.exe may exist. The function ConfirmSetPaths will test if chrome.exe exists at the "default" path on the PC (in this instance we used "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe").

If it does exist here, it starts the program. If not, it changes the variable for $GoogleChrome to be the Local App Data folder, and then executes from here.

Changes:

  1. Now uses $env variables to dictate the OS Install Path;
7
  • 1
    Worth noting you might need to Set-ExecutionPolicy -Unrestricted – DankyNanky Jan 11 '18 at 10:24
  • This is a good effort and helpful, but the OP asked how to do this in batch not powershell. Powershell adds unnecessary complication for such a simple task and your code looks more “complex” than it needs to be. When programming to “work on all systems,” you cannot make assumptions that the OS is installed on C:, or that program files is in program files (x86), or that the OS is 64-bit. See the other answer about using environment variables. – Appleoddity Jan 11 '18 at 13:41
  • 1
    I only asked for batch because it was all I was aware of (as I'm an old-fart). I am definitely open to suggestions such as the one Michael has put forward. Such suggestions expose us to new ideas. – YOURMOM Jan 12 '18 at 6:27
  • 1
    For anyone else that lands on this page from Google: The powershell file extension is .ps1 – YOURMOM Jan 12 '18 at 6:46
  • 1
    HMM, syntax error on my behalf. I shall re-evaluate when home :) – DankyNanky Jan 12 '18 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.